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++;
}