Results 1 to 11 of 11
  1. #1
    BlackZawisza is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default 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?");
    System.out.println(name);
    System.exit(0);
    }
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Making a program to print "Hello [name]!"

    To print out the output string that you want you can just concatenate the string.

    Java Code:
    System.out.println("Hello, " + name + "!");
    Or you can use the System.out.printf() method instead of just println() method.

    Java Code:
    System.out.printf("Hello, %s!", name);

  3. #3
    BlackZawisza is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Making a program to print "Hello [name]!"

    Does this work okay?

    public class DialogViewerApp
    {
    public static void main(String[] args)
    {
    // Ask user for name
    String name = JOptionPane.showInputDialog("What is your name?");
    // Print message Hello name
    System.out.print("Hello, ");
    System.out.print(name);
    System.out.println("!");
    System.exit(0);
    }
    }

    When I try and run it in NetBeans I get this error:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: JOptionPane.showInputDialog at dialogviewerapp.DialogViewerApp.main(DialogViewerA pp.java:19)

    Please respond as soon as you can, the deadline is inching closer and I'm getting a little desperate...

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Making a program to print "Hello [name]!"

    Yes, you can print it out like that.

    Regarding the error message have you import the JOptionPane in your code?

  5. #5
    BlackZawisza is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Making a program to print "Hello [name]!"

    Oh, I'm running all this in NetBeans 7.3, for the record.

    I'm not sure what I did to solve the above error, but I seem to have inadvertently done so.

    I have a new error message, though: "Could not find or load main class dialogviewer.DialogViewer"

    I get the feeling this is a really easy problem to fix, but I can't remember how. Suggestions?

    PS: You are being REALLY helpful, thanks for saving my butt =)

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Making a program to print "Hello [name]!"

    What is your actual class name? Is it DialogViewer or DialogViewerApp?

  7. #7
    BlackZawisza is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Making a program to print "Hello [name]!"

    DialogViewerApp

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Making a program to print "Hello [name]!"

    That's why you got this error: "Could not find or load main class dialogviewer.DialogViewer". Your main class is not dialogviewer.DialogViewer but dialogviewer.DialogViewerApp instead. So tell netbeans to execute the right command to load your main class.

  9. #9
    BlackZawisza is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Making a program to print "Hello [name]!"

    Thanks a lot! The program works great in NetBeans. There's one last problem, though - when I try to run it in the Windows Command Processor using these instructions (How to Compile Java Programs through Windows CMD | a Tech-Recipes Tutorial), it gives me the following error after I enter C:\Java>java DialogViewerApp in Step 9...

    Exception in thread "main" java.lang.NoClassDefFoundError: DialogViewerApp (wrong name: dialogviewerapp/DialogViewerApp)
    at java.lang.ClassLoader.defineClass1(Native Method)

    it has a bunch more lines of the "at java.lang", mostly containing more class loader stuff. Do you know what might be causing this? Please let me know if you need me to post the entire error message.

  10. #10
    BlackZawisza is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Making a program to print "Hello [name]!"

    I've tried running a different program in the Command Processor the same way, and I get essentially the same error, if that helps.

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Making a program to print "Hello [name]!"

    When you run it from the command line, it expects the class file to be found in a directory named after the package name, relative to the classpath. So if your classpath is "." (the current directory), then it expects a class called dialogviewerapp.DialogViewerApp to be found in ./dialogviewerapp/DialogViewerApp.class. If DialogViewerApp.class is in the current directory and you type 'java DialogViewerApp', it finds the class file but then looks in it and finds that it is called dialogviewerapp.DialogViewerApp instead of just DialogViewerApp, and gives you the error you are seeing.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 1
    Last Post: 02-01-2012, 09:37 PM
  3. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 06:40 PM
  4. print pattern as "1 a 2 b 3 c 4 d 5 e" from following program
    By mukeshgulia in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-31-2010, 09:25 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •