Results 1 to 14 of 14
  1. #1
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default [solved] Please help...Stuck on first project.

    Hi, I'm currently taking my first Java class right now and I have a project due by next week. I need to write a script using JOptionPane to prompt user for their name (last, first name) and age (#) for two persons. Here's what I got so far, otherwise, I'm stuck. Hopefully you coding gurus could give me a hand. Thanks

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Hw1
    {
    	public static void main( String[] args )
    	{
    		// prompt user to enter name and age
    		String name1 =
    			JOptionPane.showInputDialog( "Please enter your last, and first name" );
    		int age1 =
    			JOptionPane.showInputDialog( "Please enter your age" );
    		String name2 =
    			JOptionPane.showInputDialog( "Please enter your last, and first name" );
    		int age2 =
    			JOptionPane.showInputDialog( "Please enter your age" );
    Last edited by tonie; 06-13-2013 at 08:50 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: Please help...Stuck on first project.

    So exactly what are you stuck on? What are you expected to do next? And what have you learned in your class about input and output?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: Please help...Stuck on first project.

    When everything is done, a dialog box should display the following format:

    1st person: Parker, Peter (25)
    2nd person: Wayne, Bruce (32)
    Bruce is older than Peter
    I need to use separate dialog boxes for each input.
    This is only my second class so I'm pretty much lost.
    Please understand I'm not trying to get you to do my work but hopefully lead and help me.

    Thanks

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Please help...Stuck on first project.

    output is displayed with JOptionPane.showMessageDialog(null, "1st...."); method
    So, use three output dialog boxes like you did with the input.

  5. #5
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: Please help...Stuck on first project.

    Ok, so I got this far. How do I input the names and how can I get the show message to display the proper message? Please let me know. Thanks

    Edit: I edited the "message" and now I'm getting errors. I know it's wrong, but just don't know how to troubleshoot it.

    Here's part of the requirements.

    You should store the names in String variables and the ages in int variables.

    import javax.swing.*;
    import java.text.*;

    public class Hw1
    {
    public static void main( String[] args )
    {
    // declare the variable
    int age1, age2;
    String lastName1;
    String firstName1;
    String lastName2;
    String firstName2;


    // prompt user to enter name and age
    lastName1 = JOptionPane.showInputDialog( "Please enter your last name." );
    firstName1 = JOptionPane.showInputDialog( "Please enter your first name." );
    lastName2 = JOptionPane.showInputDialog( "Please enter your last name." );
    firstName2 = JOptionPane.showInputDialog( "Please enter your first name." );

    age1 = Integer.parseInt( JOptionPane.showInputDialog( "Please enter your age" ) );
    age2 = Integer.parseInt( JOptionPane.showInputDialog( "Please enter your age" ) );

    String message = "";

    message = "lastName1, firstName1 is ", + age1 + "lastName2, fistName2 is ", + age2;

    // compare the two ages
    if ( age1 > age2 )
    message = message + "\nage1 is older than age2";

    if ( age1 < age2 )
    message = message + "\nage1 is younger than age2";

    if ( age1 == age2 )
    message = message + "\nage1 is of same age as age2";

    JOptionPane.showMessageDialog( null, message );

    System.exit( 0 );


    } // main
    } // Welcome
    Last edited by tonie; 06-13-2013 at 04:42 AM.

  6. #6
    jamgor is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Please help...Stuck on first project.

    What errors are you exactly getting? Does your compiler give you a list of errors?

  7. #7
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: Please help...Stuck on first project.

    Quote Originally Posted by jamgor View Post
    What errors are you exactly getting? Does your compiler give you a list of errors?
    I apologize, I forgot to include the errors.

    C:\Users\Mat\Desktop\insy>javac Hw2.java
    Hw2.java:27: ';' expected
    message = "lastName1, firstName1 is ", + age1 + "lastName2, fistName2 is ", + ag
    e2;
    ^
    Hw2.java:27: not a statement
    message = "lastName1, firstName1 is ", + age1 + "lastName2, fistName2 is ", + ag
    e2;
    ^
    Hw2.java:27: ';' expected
    message = "lastName1, firstName1 is ", + age1 + "lastName2, fistName2 is ", + ag
    e2;
    ^
    Hw2.java:27: not a statement
    message = "lastName1, firstName1 is ", + age1 + "lastName2, fistName2 is ", + ag
    e2;
    ^
    4 errors

  8. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Please help...Stuck on first project.

    regarding to the error you are getting, it is because you have commas after the " Delete commas.

    now it is ok
    message = "lastName1, firstName1 is " + age1 + "lastName2, fistName2 is " + age2;

    another tip what you put in the string between the " " is displayed exactly like that, your message will therefore display lastName1, firstName1 instead of the actual names

  9. #9
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: Please help...Stuck on first project.

    thanks superhands! I got it how I want it to display. Now I just don't know how to sub in the names with the input names. I tried to remove the quotes, but it gave me error. I would appreciate any help. Thanks

    import javax.swing.*;
    import java.text.*;

    public class Hw1
    {
    public static void main( String[] args )
    {
    // declare the variable
    int age1, age2;
    String lastName1;
    String firstName1;
    String lastName2;
    String firstName2;


    // prompt user to enter name and age
    lastName1 = JOptionPane.showInputDialog( "Please enter your last name." );
    firstName1 = JOptionPane.showInputDialog( "Please enter your first name." );
    lastName2 = JOptionPane.showInputDialog( "Please enter your last name." );
    firstName2 = JOptionPane.showInputDialog( "Please enter your first name." );

    age1 = Integer.parseInt( JOptionPane.showInputDialog( "Please enter your age" ) );
    age2 = Integer.parseInt( JOptionPane.showInputDialog( "Please enter your age" ) );

    String message = "";

    message = "First Person: " + "lastName1, firstName1 is " + age1 + " \nSecond Person: lastName2, firstName2 is " + age2;

    // compare the two ages
    if ( age1 > age2 )
    message = message + "\nage1 is older than age2";

    if ( age1 < age2 )
    message = message + "\nage1 is younger than age2";

    if ( age1 == age2 )
    message = message + "\nage1 is of same age as age2";

    JOptionPane.showMessageDialog( null, message );

    System.exit( 0 );


    } // main
    } // Welcome

  10. #10
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Please help...Stuck on first project.

    the variables should not be in quotes, the commas and the text you type should be in quotes

    Java Code:
    message = "First Person: " + lastName1+", "+ firstName1 +" is " + age1 + " \nSecond Person:"+ lastName2+", "+ firstName2 +" is " + age2;
    there is a problem in the message you add in the comparison, you only add a string that says age1 is older that age2.
    I suppose you want the names, namely, person a is older than person b. Again, you need to put your variable + "is older than" + variable

  11. #11
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: Please help...Stuck on first project.

    What's wrong with this?



    message = "First Person: " + lastName1+", " + firstName1 + " is " + age1 + " \nSecond Person: " + lastName2 + ", " firstName2 + " is " + age2;

    // compare the two ages
    if ( age1 > age2 )
    message = message + \nlastName1 + " is older than " + lastName2;

    if ( age1 < age2 )
    message = message + \nlastName1 + " is younger than " + lastName2;

    if ( age1 == age2 )
    message = message + \nlastName1 + " is of same age as " + lastName2;

    message = "First Person: " + lastName1+", " + firstName1 + " is " + age1 + " \nSecond Person: " + lastName2 + ", " firstName2 + " is " + age2;

    // compare the two ages
    if ( age1 > age2 )
    message = message + \nlastName1 + " is older than " + lastName2;

    if ( age1 < age2 )
    message = message + \nlastName1 + " is younger than " + lastName2;

    if ( age1 == age2 )
    message = message + \nlastName1 + " is of same age as " + lastName2;

  12. #12
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Please help...Stuck on first project.

    ", " firstName2 should be ", " + firstName2

    + \nlastName1 should be + "\n" + lastName1

    also, post code in CODE tags

  13. #13
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: Please help...Stuck on first project.

    superhands!!! Thank you so much! I finally got it with your help and I've learned so much from this! :-)

    BTW, how do I post code in CODE tags?

  14. #14
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Please help...Stuck on first project.

    No problem
    you add code tags like this

    [CODE ] the code [/CODE ]

Similar Threads

  1. Replies: 3
    Last Post: 02-12-2013, 03:21 PM
  2. Re: I'm new and stuck, please help :(
    By leader111 in forum New To Java
    Replies: 2
    Last Post: 09-25-2012, 10:38 PM
  3. I stuck my project from any piece
    By papyon in forum Java 2D
    Replies: 3
    Last Post: 12-05-2011, 08:06 PM
  4. I'm stuck help!!!
    By nobody58 in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 02:52 PM
  5. Im on my last lab!!!! And im stuck...:(
    By clanboru15 in forum New To Java
    Replies: 5
    Last Post: 03-13-2009, 01:44 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
  •