While | Sololearn: Learn to code for FREE!

+1

While

How to exit the while loop. By writing the word "exit" by the user? String str = new String(); Scanner scanner = new Scanner(System.in); while (str.length() == 0) { System.out.print(“Please enter something”); str = scanner.nextLine(); }

1/30/2022 3:13:22 PM

Игорь Панасик

7 Answers

New Answer

+1

if(str.equals("exit")) scanner.close();

+1

        [No Name] Nevermind,I was just typing that "Break" statement also work there.

0

0

        [No Name] What about break statement?

0

Manav Roy Yes, but you can only use the break keyword inside the loops or switch statements. Note that if the break is placed in inner loop then only the inner loop will stop. If you place break in outer loop then all the loops will stop

0

        [No Name] And Игорь Панасик asking regarding while loop.

0

Manav Roy Sorry but I am not sure what are you asking. Any brief explanation may help.