0

I have the following piece of code, and I'm trying to use the input entered by the user in if/else statements later on:

String userGuess = JOptionPane.showInputDialog("The first card is " 
    + firstCard + ". Will the next card be higher, lower or equal?");

How can I use the word they input, i.e. "higher", "lower" or "equal" outside of the if/else statement that this code is in? The code I need their answer for is:

if (userGuess == "higher" && nextCard > firstCard)
{
    String userGuess = JOptionPane.showInputDialog(null, "Correct! The current card is a " 
               + nextCard + ". Will the next card be higher, lower or equal?");
    correctGuesses++;
}

EDIT: Thank you for your help, I figured it out!

user2928362
  • 27
  • 1
  • 7
  • 6
    You should use `equals` instead of `==` to compare strings, see: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?rq=1 – micha Nov 06 '13 at 20:12
  • 1
    `.equalsIgnoreCase` is how we compare `Strings`. – Tdorno Nov 06 '13 at 20:12

3 Answers3

1

Try this code:

if (userGuess.equalsIgnoreCase("higher") && nextCard > firstCard)
{
    String userGuess = JOptionPane.showInputDialog(null, "Correct! The current card is a " 
           + nextCard + ". Will the next card be higher, lower or equal?");
    correctGuesses++;
}

else if (userGuess.equalsIgnoreCase("higher") && nextCard == firstCard)
{
    String userGuess = JOptionPane.showInputDialog(null, "Correct! The current card is a " 
               + nextCard + ". Will the next card be higher, lower or equal?");
    correctGuesses++;
}

else if (userGuess.equalsIgnoreCase("lower") && nextCard < firstCard)
{
    String userGuess = JOptionPane.showInputDialog(null, "Correct! The current card is a " 
               + nextCard + ". Will the next card be higher, lower or equal?");
    correctGuesses++;
}

String is not a primitive type. You cannot use == instead use:

if (userGuess.equalsIgnoreCase("higher") && nextCard > firstCard)
{

Take a look at Oracle's documentation on Strings. This should give you further help. Happy coding!

Galen Nare
  • 376
  • 2
  • 4
  • 18
0

If you declare the variable userGuess outside of the if-statement, and just assign it inside, then you will be able to use it outside of the if-statement.

Also, as was stated elsewhere, you should be comparing string with equals(), not ==.

gla3dr
  • 2,179
  • 16
  • 29
  • Wrong. That's not their problem. Their `if ()` statement is not testing correctly. They have already done that. – Galen Nare Nov 06 '13 at 20:16
  • That is true and will cause problems, but the actual question they were asking was about the scope of the variable was it not? – gla3dr Nov 06 '13 at 20:19
  • @Galen Nare I see what you mean now. Reading over it again, he has 2 variables named userGuess. – gla3dr Nov 06 '13 at 20:29
0

There are two decent ways:

  1. Change the name of the variable (so it doesn't conflict with the existing userGuess variable) and declare it outside of the if statement.

    String nextGuess = "";
    if (userGuess.equals("higher") && nextCard > firstCard) {
        nextGuess = JOptionPane.showInputDialog(null, "Correct! The current card is a " + nextCard + ". Will the next card be higher, lower or equal?");
        correctGuesses++;
    }
    
  2. Just use the same userGuess variable every time you have the user enter something.

    if (userGuess.equals("higher") && nextCard > firstCard) {
        userGuess = JOptionPane.showInputDialog(null, "Correct! The current card is a " + nextCard + ". Will the next card be higher, lower or equal?");
        correctGuesses++;
    }
    
crownjewel82
  • 437
  • 2
  • 12