Reading nextInt() from a text file on a new line?

How can I do this?

I have a textfile that looks like this:

0000
0000
0000

How can I read the integers in the file on the next line using nextInt() because it gets to the end of the first line and errors occur.

I know there must be a simple solution for this but I just have a huge mental block at the moment

Integer.parseInt(foo.nextLine());

Not sure if I’m using it correctly

while(input.hasNext())
		{
		
				
			
			int n = Integer.parseInt(input.nextLine());
			System.out.print(n);
				

		}

Only reading one line

Use System.out.print[b]ln/b


try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
    String line;
    while ((line = reader.readLine()) != null) {
          System.out.print(Integer.parseInt(line)); 
    }
} catch (IOException | NumberFormatException e) {
    e.printStackTrace();
}

This still only prints one line from the file, but it will print it like so

0
0
0
0

So it will print one line from the file, which looks like this: 0000

Oh so it coudl be like so:



1234
2341
1235


and you want to read each digit??

Yes sir

Watch what happens when you change the numbers.

Integers don’t keep the leading zeros. Sounds like you want Strings.
EDIT: or use NumberFormat etc. to print the integers padded with leading zeros.
This is why you don’t have identical test data.

@Kudo
Use Scanner and rejoice that you never have to use a BufferedReader ever again.

No I want numbers. Guess I will stop using 0’s ???

See my edit.

When I change to 1’s

1111
1111
1111

This is all read fine with your code

But when I change this to a much larger number of 1’s I get a NumberFormatException

???


for (int i = 0; i < line.length; i++) {
    int n = Character.getNumericValue(line.charAt(i));
}

This?

Well that’s probably because int’s have a bounded size, which you exceeded. Try BigInteger if you need arbitrarily-sized numbers.

What the hell are even trying to do, exactly? This whole thread reeks of “XY problem.”

Wait so is it possible to read each integer on a line separately, not as one integer? Can I read them as separate characters?

Java is general-purpose language, you can do whatever you want.

Until you say what that is, I’m convinced you don’t know yourself and that we’re wasting our time.

I want to be able to read each integer on the line as a separate integer.

Yes thats what that loop I pasted will do.

@BurntPizza wow Scanner looks really useful for parsing my model files. Can’t believe I’ve never ran across it. Might switch over to that instead of BufferedReader and splitting/substring manipulations.

Does that mean any single line contains more than a single integer, or read multiple lines, each containing one integer?

while (input.hasNext()) {
    String line = Integer.parseInt(input.nextLine());
    for (int i = 0; i < line.length; i++) {
        int n = Character.getNumericValue(line.charAt(i));
        System.out.println(n);
    }
    System.out.println("------");
}