Runaway while loop when using .hasNext on a string

Hello,
I am working on a method that will count the number of tokens in a string, specifically integers. However, i'm finding that for some reason it is running endlessly even though there are only a small amount of tokens in the given string. I have a feeling given the small amount of code it is a conceptual misunderstanding somewhere on my part. Below is the method in question, any direction would be appreciated!


public static void countNumbers(String numstring)
{
int numbers = 0;
Scanner input = new Scanner(numstring);
while ( input.hasNext() )
{
numbers++;
System.out.println(numbers);
}

}

Comments

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion