Reputation:
I am trying to read a file which contains integer values in java and i have found the following answer for this:
Java: Reading integers from a file into an array
But my code stuck in infinite loop.
My code is :
Scanner scanner = new Scanner(new File("/home/lalit/Desktop/Project/key_x.txt"));
//Scanner scanner1 = new Scanner(new File("/home/lalit/Desktop/Project/key_y.txt"));
int i = 0,count=0;
while(scanner.hasNextInt())
{
System.out.println("This is count "+count);
count++;
}
The above code goes in infinite loop.
My text file contains :
317
40
280
10
318
24
456
126
4
129
404
468
287
275
165
I got one more problem in this, when i try to store the elements in int array then it throws exception ;
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Hybrid.Image.main(Image.java:503)
my code of storing array elements is :
int tall[] =new int[count];
int tall1[] =new int[count];
while(scanner.hasNextInt())
{
int k=scanner.nextInt();
tall[i] =k;
System.out.println(tall[i]);
i++;
}
Upvotes: 0
Views: 9613
Reputation: 3955
Change your code as shown below because you are never fetching the value.
Scanner scanner = new Scanner(new File("/home/lalit/Desktop/Project/key_x.txt"));
//Scanner scanner1 = new Scanner(new File("/home/lalit/Desktop/Project/key_y.txt"));
int i = 0,count=0;
while(scanner.hasNextInt())
{
scanner.nextInt()
System.out.println("This is count "+count);
count++;
}
Upvotes: 1
Reputation: 56
You forget to move the cursor.
A scanner works by reading an input stream. You can imagine getting the entire file in one long string and then a cursor in the start of it. Then when you call .hasNextInt() you ask the scanner if there is an integer in front of the cursor at the current position. If there is it will return true but won't actually move the cursor. The cursor will only move when you call nextInt(). And scanner.nextInt() will return an integer as well.
Upvotes: 0
Reputation: 2616
When you're creating a Scanner
it has an inner state. This state can be changed when you're getting the data from it using scanner.nextXXX()
API.
scanner.hasNextInt()
doesn't change the state. Only checks it. While scanner.nextInt()
does change its state, and then in the next time you're calling it, it will fetch the next piece of data.
Upvotes: 0
Reputation: 219096
The loop never ends because the condition never changes. You're only checking if the scanner has a "next int
", you never actually read that int
. So it's always ready to read the first int
in the file.
If you don't care what that int
is and just want to read it from the scanner, you can do that and simply ignore the value:
while(scanner.hasNextInt())
{
scanner.nextInt();
System.out.println("This is count "+count);
count++;
}
Alternatively, if you do want to actually do something with that int
, it'll be in the return value of scanner.nextInt()
.
Upvotes: 4