reading input till EOF in java

后端 未结 10 637
后悔当初
后悔当初 2020-12-09 06:35

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

10条回答
  •  庸人自扰
    2020-12-09 06:47

    Scanner scanner = new Scanner(System.in);
    int c = 0;
    while(scanner.hasNext()){
      System.out.println(++c + " " + scanner.nextLine());
    }
    scanner.close();
    // use while instead of normal for loop. 
    // If you need to read a file than BufferReader is the best way to do it, as explained above.
    

提交回复
热议问题