how to read Integer from a text file in Java

1194
June 13, 2017, at 06:55 AM

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

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().

Answer 2

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

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.

Answer 4

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.

READ ALSO
I want to plot dynamic line chart using javafx

I want to plot dynamic line chart using javafx

Chart to get the line chart values for plot

476
set image bitmap to imageView adapter

set image bitmap to imageView adapter

At the moment i am using custom adapter/recycle view to build a simple image and a small galery under the image, every time when i click on one of the images below it loads the image in the main imageView above

370
How get cookies in Java @ServerEndpoint @OnMessage method?

How get cookies in Java @ServerEndpoint @OnMessage method?

I need to get the cookies from a web socket when they call sendMessageI tried adding a @WebFilter, but that's only called when they first open the web socket

416