0

I have a scanner that gets a users input x which if input correctly outputs "correct" and if the input is not right, it will output "incorrect". However whether i type the correct or incorrect string im stuck in an endless loop of the while loop outputting "incorrect".

How can i check if a users input is correct and if so, output the word "correct" and move onto the next part of the program. If the input is incorrect, output "incorrect" and repeat the loop until the input is correct.

Hopefully this makes sense!

Code:

    Scanner sc = new Scanner(System.in);

    int n = 0;
    String x = sc.nextLine();
    while(n == 0){
        if (x == "inv"){
            System.out.println("correct");
            n++;
        }else{
            System.out.println("incorrect");
        }
    }

1 Answers1

0

I needed to use x.equals("Inv") rather than x == "Inv"