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

That doesn’t even compile (although I know what you meant), and we haven’t even nailed down what the desired behavior is.

Kudo has the gist of it but yes it does not compile

I typed it out on here.

replace:


for (int i = 0; i < line.length; i++) {

with:


for (int i = 0; i < line.length(); i++) {

Thanks Kudo!

You want each digit of each line, then? (please just say what you want, ffs)

Here it is in hopefully a more intuitive style:


// java 7
for (String line : Files.readAllLines(Paths.get("file-path", Charset.defaultCharset())))
    for (char c : line.toCharArray())
        System.out.println(Character.getNumericValue(c));

BTW, this could all have been avoided if you separated each number by a space. Then nextInt() would work uniformly.

EDIT: Hell, if that’s all it is, then just you could just go back to Scanner:


// should work, although perhaps not as fast...
Pattern digit = Pattern.compile("\\d");
while (input.hasNext(digit)) // reads (almost) like English!
    System.out.println(Integer.parseInt(input.next(digit)));