0

Could you help me find the error in my code.

import javax.swing.JOptionPane;

String mortgagetype;

mortgagetype = JOptionPane.showInputDialog("What type of mortgage do you desire? (open or closed, only)"); 

if (mortgagetype == "open" || mortgagetype == "closed") {
 print("hello"); 
}

I want the program to print hello if the user inputs open or closed. However it doesn't and I don't know what the problem is.

F.Hode
  • 1
  • 1

1 Answers1

1

Instead of using mortgagetype == "something" in the if statement, use mortgagetype.equals("something").

akhy
  • 5,760
  • 6
  • 39
  • 60
  • This is a valid answer. Comparing string with `==` checks if the two strings are the *same instance*. While `.equals()` method checks if the two string have *same content*. – akhy Nov 30 '17 at 07:08