-1

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.

An Yan
  • 121
  • 3
  • 11
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Mark Melgo Mar 29 '19 at 08:15
  • it should break if `equals` is used instead of `==` and user types blank character –  Mar 29 '19 at 08:21
  • @ValentinCarnu So what you mean is I need to let the line equals to a specific string when breaking the loop? – An Yan Mar 29 '19 at 08:21
  • You need to check the user input using `equals`. For the code in the description the user needs to type blank character followed by enter to stop the app –  Mar 29 '19 at 08:23
  • @ValentinCarnu OK buddy, I fixed this. It's a tricky problem. What I write in code is line.equals(" ") and what I input is just nothing and Enter. This is the point. The solution is that I need to input " " and Enter... It is really tricky! – An Yan Mar 29 '19 at 08:29
  • So you should change your condition to `if (line.trim().isEmpty())` and it will match if the line is either whitespace or completely empty. – khelwood Mar 29 '19 at 08:42

2 Answers2

1

break should work but if not, you can try something like this:

Scanner scan=new Scanner(System.in);
String line;
int count=1;
boolean docontinue = true;
while (docontinue){
    line=scan.nextLine();
    if (line.equals(" ")){
        docontinue = false;
        System.out.println("break");
    }
    System.out.println(line);
    System.out.println(count);
    count=count+1;
}

If the break print is not working, it's because the if statement is not working too.

  • If you enter a white space before pressing enter, you should compare the line to " ".
  • If you only press enter to end the loop, you should compare the line to the empty string "". Indeed, nextLine() return the line without EOL char. So if you press enter, the line is empty with an EOL char.

Best

Maxouille
  • 2,729
  • 2
  • 19
  • 42
  • Thank you buddy. I have fixed this. What I write in code is line.equals(" ") and what I input is just nothing and Enter. This is the point. The solution is that I need to input " " and Enter... It is really tricky! – An Yan Mar 29 '19 at 08:30
  • 1
    Yep ! That's what I explained in my answer ;) – Maxouille Mar 29 '19 at 08:31
0

You need to use equals method to compare your string with empty space

if (line.equals(" ")){
    break;
}
Mark Melgo
  • 1,477
  • 1
  • 13
  • 30
Mustahsan
  • 3,852
  • 1
  • 18
  • 34