0

I'm having an infinite while loop issue, no matter what I type in even if it is "Filed" or "Incomplete" the loop keeps re prompting and I can't figure out why.

strMajorSheet = JOptionPane.showInputDialog(null,"What is the advisee's major sheet status? (Filed/Incomplete)",
                "Advisee's Major Sheet",3);

if(strMajorSheet == "Filed" || strMajorSheet == "Incomplete")
                    {

                        switch(strMajorSheet)
                        {
                            case "Filed":
                                blnMajorSheet = true;
                            case "Incomplete":
                                blnMajorSheet = false;


                        }
                    }
                    else
                    {
                        while(strMajorSheet != "Filed" && strMajorSheet != "Incomplete")
                        {
                            strMajorSheet = JOptionPane.showInputDialog(null,"What is the advisee's major sheet status? (Filed/Incomplete)",
                                "Advisee's Major Sheet",3);


                        }
Charles
  • 50,943
  • 13
  • 104
  • 142

3 Answers3

0

String comparison Java must be done using equals and equalsIgnoreCase

0

please don't compare String using ==

change it to

if(strMajorSheet.equals("Filed") || strMajorSheet.equals("Incomplete"))
......

while(!strMajorSheet.equals("Filed") && !strMajorSheet.equals("Incomplete"))
......
Baby
  • 5,062
  • 3
  • 30
  • 52
0

First of all, String comparison is not done using ==, which compares the object references, which are unlikely to be true very often. Instead, you need to use equals or equalsIgnoreCase to compare the contents of the two Strings.

Having said that, I don't know why you're bothering with the if statement as the entire logic can be achieved using just the switch statement, for example...

boolean gotInput = true;

do {
    String strMajorSheet = JOptionPane.showInputDialog(null, "What is the advisee's major sheet status? (Filed/Incomplete)",
                    "Advisee's Major Sheet", 3);

    gotInput = true;
    switch (strMajorSheet) {
        case "Filed":
            blnMajorSheet = true;
            break;
        case "Incomplete":
            blnMajorSheet = false;
            break;
        default:
            gotInput = false;
    }
} while (!gotInput);

Now, beware, that switch (String) is the same as using String#equals, that is, it's case sensitive comparison.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366