I'm using the Scanner class as stdin for read input from keybord. I would read token by token the string insert until the end of the line. token are separated by white spaces.
this is my code:
Scanner in = new Scanner(System.in);
int count=0;
while(in.hasNextLine()){
while(in.hasNext()){
String s=in.next();
System.out.println(s);
if(s.equals("\n"))
break;
count++;
}
break;
}
System.out.println(count);
The problem is that when i press enter, it seems considering it like a white space and it never exit from while block, so it continues to expect another input.
the only way to exit from the while and end the program in Linux is to press ctrl+D (=enter) but i'm searching another way...
I tried to insert an if statement which check if the token is a "\n" but it doesn't work.
Can you help me find a solution to the problem?