【发布时间】: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();
有什么想法吗?谢谢!
【问题讨论】:
-
使用
next而不是nextLine读取月份。 -
请将错误格式化为代码。这看起来像一团糟。
标签: java