Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

file io - reading input till EOF in java

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...