reading input till EOF in java

zer0nes picture zer0nes · Dec 18, 2012 · Viewed 87k times · Source

In C++ if I wish to read input till the EOF I can do it in the following manner

while(scanf("%d",&n))
{
    A[i]=n;
    i++;
}

I can then run this code as ./a.out < input.txt. What is the java equivalent of this code?

Answer

Ted Hopp picture Ted Hopp · Dec 18, 2012

You can do this:

Scanner s = new Scanner(System.in);
while (s.hasNextInt()) {
    A[i] = s.nextInt();
    i++;
}