简体   繁体   English

扫描仪实际上并未接受用户输入

[英]Scanner not actually taking in user input

I've tried different uses for Scanners (I want it to read in Files but I also tried just Strings), and it just skips over the code as if it doesn't exist.我尝试过扫描仪的不同用途(我希望它在文件中读取,但我也只尝试过字符串),它只是跳过代码,就好像它不存在一样。 No error messages are shown.不显示错误消息。

   public static void main(String[] args)
      throws FileNotFoundException {
      
      Scanner test = new Scanner(System.in);
      String testLine = test.next();

      Scanner input = new Scanner(new File("data.txt"));

      while(input.hasNextLine){
         String name = input.nextLine();
         String letters = input.nextLine();
         System.out.println(name + ": " + letters);
      }
   }

Your code doesn't compile, because hasNextLine() is a function, not a class member.您的代码无法编译,因为hasNextLine()是 function,而不是 class 成员。

You are actually reading from System.in at test.next();您实际上是在test.next();处从System.in读取数据; - you have to enter some text, then your code will continue to run. - 您必须输入一些文本,然后您的代码将继续运行。 It's just waiting for a user input - thus no exception is thrown.它只是在等待用户输入 - 因此不会引发异常。

At this line of code:在这行代码:

String testLine = test.next();

your program is waiting for your input.您的程序正在等待您的输入。 It cannot proceed to next line till you provide an input.在您提供输入之前,它无法进入下一行。

EDIT:编辑:
Taking cue from Charlie's comment below, here is a quote about System.in fromdocs.从下面查理的评论中得到启示,这里是来自文档的关于System.in的引用。

The "standard" input stream. “标准”输入 stream。 This stream is already open and ready to supply input data.这个 stream 已经打开并准备好提供输入数据。 Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.通常,此 stream 对应于键盘输入或主机环境或用户指定的其他输入源。

More here.更多在这里。 . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM