Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Small question! Looking for help!

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Small question! Looking for help!

    I was able to make it work fine with just the first part (asking for name and then displaying how was there day) But im trying to make it also ask how there day was while displaying the persons name, having some dificulties knowing how to get a name while also displaying the persons name.

    import javax.swing.JOptionPane;
     
    public class Day
    { 
        public static void main( String[] args )
        {
            String name = 
            JOptionPane.showInputDialog( "Oh hi, What's your name?" );
     
            String message = 
                    String.format( "Hello %s! How was your day?", name );
        }
        {
            String better = 
            JOptionPane.showInputDialog( "How could your day be better?" );
     
            String bettermessage = 
                    String.format( "Ok %s would make it better?", better );
     
            JOptionPane.showMessageDialog ( null, message );
        }
    }


    --- Update ---

    So i did some fiddling and got it to display the prompt again, but then it doesn't show the name, so im kinda inbetween!

    import javax.swing.JOptionPane;

    public class Day
    {
    public static void main( String[] args )
    {
    String name =
    JOptionPane.showInputDialog( "Oh hi, What's your name?" );

    String message =
    JOptionPane.showInputDIalog( "Hello %s! How was your day?", name );

    String better =
    JOptionPane.showInputDialog( "How could your day be better?" );

    String bettermessage =
    String.format( "Ok %s would make it better?", better );

    JOptionPane.showMessageDialog ( null, message );
    }
    }


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Small question! Looking for help!

    So what is it you are struggling with? I am thinking you are struggling to get the concept of variables?

    In your line
    String name = JOptionPane.ShowInputDialgo("Oh hi, What's your name?" );
    You declare and initialize a String variable called name. This variable holds data so you can re-use it without having to request this information again. Anytime you wish to access its contents you would simply use the name of the variable.

    Simply take a look at your code since you call it in the next line. Now since you are using String.format you can see that where %s is at it replaces it with whatever string you provide it with.

    Here is an example to show you what is happening:
    String name = "Henry";
    int age = 20;
    System.out.printf("Hello %s!", name);
    System.out.printf("Hello %s, I am also %d years old! Goodbye %s", name, age, name);

    As you can see you can use multiple variables in a single line. The letter after % specifies the type of data you are going to provide it. For example you can not %s and provide it an int or it will complain.

    I know it was not my best explanation by far but hopefully you can get the gist of what is happening. If not be a little more specific on what you are attempting to accomplish.

Similar Threads

  1. Big Rookie Question - New to Java, small problem Help appreciated!
    By Knighter in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 24th, 2013, 01:55 AM
  2. Simple question about a small program
    By azizmaiden in forum Java Theory & Questions
    Replies: 5
    Last Post: March 2nd, 2013, 05:52 PM
  3. Small problem, help please?
    By Jonathansafc in forum What's Wrong With My Code?
    Replies: 33
    Last Post: September 3rd, 2011, 06:46 PM
  4. Small Project
    By 3XiLED in forum Paid Java Projects
    Replies: 7
    Last Post: March 1st, 2010, 08:35 AM
  5. small problem ... I need help
    By Ashar in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2009, 11:26 AM