I'm using scanner to process through a user input sentence and if the word "hey" appears it adds to scanner. So basically a word count. How do I break out of the infinite while(scan.hasNext()) loop without using something like
if(scan.next().equals("exit")
break;
I can't break out the loop that way because I've been given inputs that I can't change.
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
String speedLimit;
int c = 0;
while(scan.hasNext()){
if (scan.next().equals("hey")){
c++;
}
}
System.out.println(c);
}