Making a program to print "Hello [name]!" - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Making a program to print "Hello [name]!"

I'm very very new to Java, currently taking a college course in it. I'm supposed to start with the following program, and change it so that when the user types in their name, the program will print "Hello, [name]!" I have absolutely no idea where to start, and would enormously appreciate any help you folks could give.
import javax.swing.J0ptionPane;

public class DialogViewer
public static void main(String[] args)
String name = J0ptionPane.showInputDialog("What is your name?");


  • uletus2uletus2 Posts: 3Member
    Hi there; you have reached the SOS programmer-hotline,

    Somehow you have to define "name" as a variable, like a container ship holding the letters which build the person's name.

    Also you're missing the equivalent of the BASIC INPUT$ statement.
    In Pascal this is readln as opposed to writeln which is for outputting fixed constant strings and both numeric and string variables and even calculations.

    I'm absolutely no expert on Java, I'll look it up for you... Hung on...

    System.out.print("Hello, what's your name? ");
    String name=readLine();
    System.out.println("Hello "+name+"!");
    System.out.println("More tutorials in");
    ^source on the internet where I found this.

    (the name they give the programming language is more a definition of what the programmer's basics are talked about by the compiler or interpreter, analysed I mean. It's of secondary nature and there is equivalence among the programming languages. Try typing "Haskell is useless" in the youtube search box, interesting video with Simon Peyton Jones!).
  • onepphireonepphire Posts: 2Member
    Hello BlackZawisza!
    So first things first let's start by breaking down the original code so you can understand what's going on.

    So the first line:
    import javax.swing.J0ptionPane;
    is just telling java that we would like to include the code found in the JOptionPane class that is in the package swing that is itself in the package javax

    The next line:
    public class DialogViewer
    is just defining the name of the class we are creating

    The next line:
    public static void main(String[] args)
    is telling java that here is the main part of the code to execute

    The next line:
    String name = J0ptionPane.showInputDialog("What is your name?");
    is actually starting us off. It is saying "Hey java, here I'm creating a String and I'm calling it 'name'. Then I want to set the value of it to whatever the user inputs into the InputDialog that asks 'What is your name?'."

    The next line:
    is telling Java "Hey, remember that variable called 'name' that I made? Yea well I want you to print it out."

    And lastly:
    just tells Java to exit the program and return a code of 0. Don't worry about this.

    Ok, onto modifying the code. So what we want to achieve is having the program print out "Hello, [name]". So let's go back and find the line of code that prints something out for us.

    That would be this:
    Right now the only thing it's printing out is the name. We can see that because the only parameter (the thing inside the parenthesis) is the variable we created in the line before. Let's change that to suit our needs.

    System.out.println("Hello, " + name);
    This is basically the same, the only thing we've changed is what the argument is. By adding "Hello, " we tell java to print out literally what is inside the quotes. Then we tell it to add on the variable to the end of this.

    Once that's done you should have a program that fulfills the assignments objectives. :)

    Hope this helped,
  • saocsaoc SchweizPosts: 1Member

    Hi BlackZawisZa,

    Somehow I am a java beginner too, but maybe I can show you some basic skills.

    At first you should use the java conventions. Those recommend that you should write the same things in every class.

    It begins with the signature of the class. Like follow:

    public class Test

    Then it recommends to carry on with the data fields. Don`t use a public declarations.

    private String name;

    If you want to use a constructor, it should be like follow:

    public Test(String inputName)
    name = inputName;

    At last, try to form the method as below.

    public void giveName()
    System.out.println(„Hello, “ + name);

    Never forget the semicolon at the end of the println.

    It is really basic java code, but I strongly advice you to begin without the jFrames. Try using blueJ, instead of a normal programming tool.


Sign In or Register to comment.