【问题标题】:Using a delimiter on a scanner object passed to a method在传递给方法的扫描仪对象上使用分隔符
【发布时间】:2017-05-19 19:17:49
【问题描述】:

此问题与本网站上的另一个问题相关联:

Beginner in Java - Using Parameters and Scanner

有问题的练习站点创建类并使用以下调用调用您编写的方法: inputBirthday(new Scanner("8\nMay\n1981\n "));

我很想知道为什么它也不起作用。我的方法代码是这样的:

public static void inputBirthday(Scanner scan) {
    System.out.print("On what day of the month were you born? ");
    int monInt = scan.nextInt();
    System.out.print("What is the name of the month in which you were 
    born? ");
    String monStr = scan.nextLine();
    System.out.print("During what year were you born? ");
    int year = scan.nextInt();
    scan.close();
    System.out.println("You were born on " + monStr + ", " + monInt + year 
    + ". You're mighty old!");
}

我不断收到此错误:

“NoSuchElementException:靠近输入行 3token 'May' 不能解释为 int 类型”,

就在我的代码中这一行之后:

int year = s.nextInt();

有什么想法吗?谢谢!

【问题讨论】:

标签: java


【解决方案1】:

当您输入您的出生月份并按 Enter 键时,即

System.out.print("On what day of the month were you born? ");
    int monInt = scan.nextInt();

您希望他们输入一个数字。如果他们输入 13 并按回车,计算机会看到您输入了13\n,因为您也按了回车。您对 nextInt() 的调用将读取 13,但保留 \n。 nextLine() 的工作方式是读取直到下一次出现\n,因为\n 用于结束一行。这个问题是你现在打电话 System.out.print("During what year were you born? "); int year = scan.nextInt(); 接下来,但是您仍然希望输入他们出生月份的字符串表示形式。因此,当您输入字符串时,您对 nextInt() 的调用不能被解析为整数,因为它不是整数——它是字符串! 您有两个选项可以解决此问题 - 您可以在第一次调用 nextInt() 后直接调用 scan.nextLine() 以便从输入缓冲区中删除 \n,或者您可以使用 scan .next() 而不是 scan.nextLine() 来存储月份的字符串表示形式。 next() 将(通常)读取到下一个空格,因此仍然存在于输入缓冲区中的 \n 不会引起任何问题。您的代码的功能版本如下 -

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("On what day of the month were you born? ");
        int monInt = input.nextInt();
        System.out.print("What is the name of the month in which you were born? ");
        String monStr = input.next();
        System.out.print("During what year were you born? ");
        int year = input.nextInt();
        input.close();
        System.out.println("You were born on " + monStr + ", " + monInt + year
                + ". You're mighty old!");
    }

这个程序的一个示例输出是

On what day of the month were you born? 13
What is the name of the month in which you were born? October
During what year were you born? 1943
You were born on October, 131943. You're mighty old!

请注意,您仍然需要更改文本格式以使其完全按照您的需要输出,但您之前遇到的问题已得到解决。

【讨论】:

    【解决方案2】:

    解析"8\nMay\n1981\n "
        int monInt = input.nextInt(); // Reads 8
        System.out.print("What is the name of the month in which you were born? ");
        String monStr = input.nextLine(); // Reads to end of line, aka '\n'
        System.out.print("During what year were you born? ");
        int year = input.nextInt(); // reads next token as int, which is "May"
    

    所以问题是 nextLine() 正在读取第一行的结尾,而不是 Mays 行。

    【讨论】:

      猜你喜欢
      • 2014-06-21
      • 2016-05-19
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多