Loop until valid input is reached

It runs correctly the first time, but:

  • It retains the seal "Try again (Y / N)?" no matter what login after request continue.
  • I am not sure if! = Is suitable for comparing String. I want to say, until loopChoice "is" Y or N, keep asking.

    while(isLoop) {
        // Ask for user input
        System.out.print("Enter hours worked: ");
        hoursWorked = scn.nextInt();
    
        System.out.print("Enter rate per hour: ");
        payRate = scn.nextInt();
        scn.nextLine();
    
        // Call functions to compute stuff
        ...
    
        // Print results
        ...
    
        System.out.print("\nDo you want to continue (Y/N)? ");
        loopChoice = scn.nextLine().toUpperCase();
    
        while(loopChoice != "Y" || loopChoice != "N") {
            System.out.print("\nPlease try again (Y/N)? ");
            loopChoice = scn.nextLine().toUpperCase();
        }
    
        switch(loopChoice) {
            case "Y":
                isLoop = true;
                System.out.print("\n");
                break;
    
            case "N":
                isLoop = false;
                System.out.println("Terminating program...");
                scn.close();
                System.exit(0);
                break;
    
            default:
                System.out.println("Your input is invalid!");
                isLoop = false;
                System.out.println("Terminating program...");
                scn.close();
                System.exit(0);
                break;
        }
    }
    
+3
source share
5 answers

This is not how you compare strings in Java.

There is also a logical error in the code, since the line cannot be Y and N at the same time, you should use &&instead ||. As long as the selection is neither Y nor N, you want to continue the loop. If this is any of them, you want to stop. So, the &&right choice.

, , .equals(obj)

while (!loopChoice.equals("Y") && !loopChoice.equals("N")) {

, == , . (, , , ) , .equals .

NullPointerException , :

while (!"Y".equals(loopChoice) && !"N".equals(loopChoice)) {
+1

String equals

while (!loopChoice.equals("Y") && !loopChoice.equals("N"))

,

+2

loopChoice != "Y", "Y" . :

  • loopChoice != 'Y',
  • "Y".equals(loopChoice)

"Y".equalsIgnoreCase(loopChoice).

, Java 1.6 . .

+1

, OR true, , , Y, so you ask if the input is not equal Y, false, if the input not equal N, True, ( True || False = True), while

( Y && N)

+1

. ( [] (http://docs.oracle.com/javase/tutorial/java/data/comparestrings.html)

, , , loopChoice , "Y", "N", false. , loopChoice "Y".

:

System.out.print("\nDo you want to continue (Y/N)? ");
// get value of next line, and trim whitespace in case use hit the spacebar
loopChoice = scn.nextLine().trim();


while (!("Y".equalsIgnoreCase(loopChoice) || "N".equalsIgnoreCase(loopChoice)) {
    System.out.print("\nPlease try again (Y/N)? ");
    loopChoice = scn.nextLine().toUpperCase();
}

, . , , - String1.equalsIgnoreCase(String2).

+1
source

All Articles