在Java中读取输入直到EOF [英] reading input till EOF in java

查看:238
本文介绍了在Java中读取输入直到EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,如果我希望读取输入,直到EOF,我可以按照以下方式执行:

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

然后我可以将这段代码运行为./a.out< input.txt中。什么是这个代码的java等价物?

解决方案

您可以这样做:

 扫描仪s =新扫描仪(System.in); (s.hasNextInt()){
A [i] = s.nextInt();
i ++;
}


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?

解决方案

You can do this:

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

这篇关于在Java中读取输入直到EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆