Home > Enterprise >  While condition not met, but still continuing
While condition not met, but still continuing

Time:11-27

I wrote a code for inputting integers as strings and wanted to end the loop when I am inputting a "0". I tried it and it didn't work. I am inputting a zero, it is printing "Next", it is printing the zero means that it is saving the zero in the "inputting" variable, but the loop is still continuing.

The code(Without Main and class):

import java.util.*;

String inputting = "";
            
       while(inputting != "0")
       {
         Scanner scan = new Scanner(System.in);
         inputting = scan.next();
         System.out.println("Next");
         System.out.println(inputting);
       }

What could be the reason? How can I maybe improve it that the loop stops continuing?

Thanks

I tried to input as many characters as possible, but when a zero comes, it should stop. It didn't stop.

CodePudding user response:

Is creating a new Scanner object every iteration neccessary? Also try use

inputting.equals("0")
  • Related