简体   繁体   English

如何在主要方法和方法中使用扫描仪

[英]How to use Scanner in both main and method

I'm trying to get input in both my main and other methods, but I'm not clear on how to get the scanner working in both. 我正在尝试同时在主要方法和其他方法中获得输入,但是我不清楚如何使扫描仪在两种方法中均能正常工作。

It gives me a weird error: 它给了我一个奇怪的错误:

Exception in thread "main"
java.util.NoSuchElementException
 at java.util.Scanner.throwFor(Unknown Source)
 at java.util.Scanner.next(Unknown Source)
 at java.util.Scanner.nextDouble(Unknown Source)
 at Hwk11.getRainData(Hwk11.java: 28)
 at Hwk11.main(Hwk11.java: 18)

Code: 码:

import java.util.Scanner;
public class Hwk11 {
  public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    System.out.println("How many weeks of data do you have?");
    int numWeeks = stdin.nextInt();
    if (numWeeks <= 0 || numWeeks > 52) {
      System.out.println("Invalid number of weeks.");
    }
    else {
      double[] rainWeeks = new double [numWeeks];
      getRainData(rainWeeks);
      showRain(rainWeeks);
    }
  }

  public static void getRainData(double[] rainFall) {
    Scanner stdin = new Scanner(System.in);
    System.out.println("Enter the weekly rainfall for each week.");
    for (int index = 0; index < rainFall.length; index++) {
      System.out.println("Week number " + (index + 1) + ":");
      rainFall[index] = stdin.nextDouble();
    }
  }

  public static void showRain(double[] rainFall) {
    for (int index = 0; index < rainFall.length; index++) {
      System.out.print(rainFall[index] + " ");
    }
  }
}

People are saying "works for me". 人们说“为我工作”。

The problem that the behavior (whether it works or not) depends on exactly how input is provided. 行为(无论它是否起作用)的问题完全取决于提供输入的方式。

  • If you provide input interactively, it will probably work. 如果以交互方式提供输入,则可能会起作用。
  • If you provide input by redirecting standard input like this: 如果通过像这样重定向标准输入来提供输入:

      java Hwk11 < input.txt 

    then it won't. 那就不会了

The problem is that a Scanner will read-ahead and buffer any characters available from its input stream. 问题是Scanner将预读并缓冲其输入流中可用的任何字符。 That is fine normally, but in your code you have created two distinct Scanner objects to read from System.in . 通常没关系,但是在您的代码中,您创建了两个不同的Scanner对象以从System.in读取。 Thus when standard input is redirected: 因此,当标准输入被重定向时:

  • The first Scanner.nextInt call will cause most / all of the input to be buffered in the first Scanner 第一个Scanner.nextInt调用将导致大多数/所有输入被缓冲在第一个Scanner
  • When the second Scanner is created and Scanner.nextDouble is called, it won't see the input buffered in the first Scanner and that will lead to an exception ... when it runs out of input characters "too soon". 当创建第二个Scanner并调用Scanner.nextDouble ,它将看不到第一个Scanner缓冲的输入,这将导致异常……当输入字符“太早”用完时。

The solution is to NOT create multiple Scanner objects for the same input stream. 解决方案是不要为同一输入流创建多个Scanner对象。 Use one Scanner and either put it in a field, or pass it as a parameter to all of the places that it needs to be used. 使用一个Scanner并将其放在字段中,或将其作为参数传递到需要使用它的所有位置。

Works for me. 为我工作。 Don't enter anything but a double if you asking for next double so no "2.3 cm", just pass 2.3 and add cm when you print in 如果您要输入下一个双精度数,则不要输入任何双精度数,因此不要输入“ 2.3 cm”,只需输入2.3并在打印时添加cm即可

I am assuming your showRain(double[] rainFall) method probably executes before getRaindata(double[] rainFall) is able to finish and populate the array. 我假设您的showRain(double[] rainFall)方法可能在getRaindata(double[] rainFall)能够完成并填充数组之前执行。 Array you passsing to showRain(double[] rainFall) might be empty. 您传递给showRain(double[] rainFall)可能为空。

Try putting your method call for showRain(double[] rainFall) after loop in getRanData(double[] rainFall) 尝试在showRain(double[] rainFall)循环getRanData(double[] rainFall)showRain(double[] rainFall)的方法调用放入

Alternitivly try passing the whole Scanner object to method. 或者尝试将整个Scanner对象传递给方法。

public static void getRainData(double[] rainFall, Scanner stdin) {
  //Scanner stdin = new Scanner(System.in);
  System.out.println("Enter the weekly rainfall for each week.");
  for (int index = 0; index < rainFall.length; index++) {
    System.out.println("Week number " + (index + 1) + ":");
    rainFall[index] = stdin.nextDouble();
  }
  showRain(rainFall); 
}

Don't forget to close it when you done with scanner. 完成扫描仪操作后,请不要忘记关闭它。

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

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