-1

I want the system to accept the characters M or S both upper and lowercase but it keeps looping endlessly. Where did i go wrong?

    MS1 = JOptionPane.showInputDialog("Please enter Marital Status M or S. ");
    while(MS1.substring(0) != "S" && MS1.substring(0) != "s" && MS1.substring(0) != "M" && MS1.substring(0) != "m"){
        JOptionPane.showMessageDialog(frame, "Please enter S or M.only");
        MS1 = JOptionPane.showInputDialog("Please enter Marital Status M or S. ");
    }

1 Answers1

0

we can compare only null with string usin == or != ,The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with ==, but it's better not to rely on that.

while(MS1.substring(0).equalsIgnoreCase("S") && MS1.substring(0).equalsIgnoreCase("M") {
sud
  • 505
  • 1
  • 4
  • 12
  • Please include some text explaining why you think this might solve the OP's problem. Help others to understand. – APC Dec 01 '15 at 05:28