0
import javax.swing.JOptionPane;

public class Example {
    public static void main(String[] args) {
        String colour = JOptionPane.showInputDialog("What's your favourite colour?");
        int shade = Integer.parseInt(JOptionPane.showInputDialog("From a scale of 1-10, 10 being the darkest and 1 being the lightest, what shade do you like?"));
        String response = "";

        if (colour == "Red") { 
            if (shade < 5)
                response = "Light Red";
                else 
                    response = "Dark Red";
            JOptionPane.showMessageDialog(null, "Your colour is " + response);
        }
    }
}

Why doesn't the final JOptionPane message show up? For example, the user enters "Red" and then "1", the response isn't showing up as "Light Red".

vahdet
  • 6,357
  • 9
  • 51
  • 106
  • 2
    because the whole `if` block is not executing since `colour` is not the same **instance** as `"Red"` - try using `if (colour.equals("Red")) {` https://stackoverflow.com/q/513832/85421 ; and maybe you should move the `JOptionPane` line one line down, outside the `if` block to also get an output if user did not enter "Red" (setting a default `response`) – user85421 Jun 23 '19 at 12:16
  • @CarlosHeuberger I've included your second suggestion in my answer, hope that's alright. – Matthew Jun 23 '19 at 12:30
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Progman Jun 23 '19 at 12:58

1 Answers1

1

The most likely explanation is that your code flow is never entering the parent if block that shows the message dialogue. As the comment on your question stated, this is most likely due to the fact that you are comparing two string using == operator instead of the equals(String) method. Read here for more information about the difference between these two ways of comparing strings.

This is the short summary of the answer linked above:

The equals() method compares the "value" inside String instances (on the heap) irrespective if the two object references refer to the same String instance or not.

The "==" operator compares the value of two object references to see whether they refer to the same String instance.

This is how your code should look like:

String response = "Unknown";

if (colour.equals("Red")) {
    response = shade < 5 ? "Light Red" : "Dark Red";
}
JOptionPane.showMessageDialog(null, "Your colour is " + response);

Note that I've taken some liberties in shortening down the assignment of new value to the response field (as this should make it a more compact and readable) and moved the showMessageDialog method outside the if block (as @CarlosHeuberger suggested) so that it provides a message even when the color the user has selected is not red.

Community
  • 1
  • 1
Matthew
  • 1,905
  • 3
  • 19
  • 26
  • Thanks for the help and extra info, really appreciate it. – doloresanalysis Jun 25 '19 at 10:55
  • You're welcome and since you are new to Stack Overflow I would recommend reading [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). Accepting an answer is not meant to be a definitive and final statement indicating that your question has now been answered perfectly. It simply means that you received an answer that worked for you personally. Also read [What does it mean when answer is accepted?](https://stackoverflow.com/help/accepted-answer). – Matthew Jun 25 '19 at 11:55