Java Scanner won't “finish” reading input

前端 未结 4 1663
不思量自难忘°
不思量自难忘° 2021-01-14 17:09

I\'ve been having trouble using java\'s Scanner class. I can get it to read my input just fine, but the problem is when I want to output something. Given multiple lines of i

4条回答
  •  醉酒成梦
    2021-01-14 17:25

    You need to tell the program that there is going to be no more input. This is done by appending an EOF character. This can be done manually on Linux by pressing Ctrl-D in the console. I think on Windows you can press Ctrl-Z. The stream will be automatically closed if you are piping input from one program to another.

    eg.

    cat filename | java MyJavaProgram
    

提交回复
热议问题