0

I need help with my code: I'm brand new to Java

How do I get the string "user" variable to display my choice after I've entered the choice?

import javax.swing.JOptionPane;                 
import java.util.Scanner;
public class Assignment {
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner( System.in );
        String me = JOptionPane.showInputDialog("Please input name!");
        System.out.println("Your name is: " + me);
        String user = JOptionPane.showInputDialog("Please enter your choice!");
        System.out.println("Your choice is: " + user);
        JOptionPane.showMessageDialog(null, "Your choice is: " + user);

            if(user.equals('1'))

            JOptionPane.showMessageDialog(null, "Username" + me + user);
    }  
}
1
  • you're pretty close already. look in JOptionPane class Commented Dec 4, 2013 at 1:26

1 Answer 1

3

Use double quotes when comparing strings

 if(user.equals("1"))  // double quotes

            JOptionPane.showMessageDialog(null, "Username" + me + user);
Sign up to request clarification or add additional context in comments.

6 Comments

That hasn't worked. It still wont display the message I want when I type 1 into the input box :/
It works for me. I used you exact code, replacing the single quotes with double quotes. It pops up a message "usernamepaul1" "Username" + me + user. That's how you have it. You should add spaces for the message to be more clear "Username " + me + " " + user
You need to press ok again after the user message displays, to see the next message
I typed in Paul for the first dialog, the 1 for the second dialog. Then a dialog pops up showing 1, I press Ok, then another dialog pops up showing '"usernamepaul1"`. Works.
If you don't want the user number message dilaog to pop up, just delete this line JOptionPane.showMessageDialog(null, "Your choice is: " + user);, that the dialog popped up will show '"usernamepaul1"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.