I want to input something via scanning next line in a loop and at some conditions break the loop. But I found I can't break out and still in this loop.
public class test{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
String line;
int count=1;
while (true){
line=scan.nextLine();
if (line.equals(" ")){
break;
}
System.out.println(line);
System.out.println(count);
count=count+1;
}
}
}
What is expected is like this:
apple
soda
and then a space line
And the output would be:
apple
1
soda
2
and break out and end the program
But the actual is the loop continues until I end the terminal.