我很好奇java.util.InputMismatchException的错误是什么以及为什么我会得到它。此程序是名为Dog.java的文件的驱动程序类,它接收狗的信息并将其存储为字符串。此文件直接获取信息,然后输出信息。
我投入的数据是:
Disco Bandito
4
Sally Struthers
5
Moosen
87
这是我的代码:
import java.util.Scanner;
public class Kennel {
public static void main(String[] args) {
String value1 = null;
int value2 = 0;
String value3 = null;
int value4 = 0;
String value5 = null;
int value6 = 0;
//takes the input from a text file
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){
value1 = scanner.nextLine();
value2 = scanner.nextInt();
value3 = scanner.nextLine();
value4 = scanner.nextInt();
value5 = scanner.nextLine();
value6 = scanner.nextInt();
}
//the three "dogs" in a kennel
Dog Dog1 = new Dog();
Dog1.setName(value1);
Dog1.getName();
Dog1.setAge(value2);
Dog1.getAge();
Dog1.toString();
Dog Dog2 = new Dog();
Dog2.setName(value3);
Dog2.getName();
Dog2.setAge(value4);
Dog2.getAge();
Dog2.toString();
Dog Dog3 = new Dog();
Dog3.setName(value5);
Dog3.getName();
Dog3.setAge(value6);
Dog3.getAge();
Dog3.toString();
System.out.println(Dog1.toString());
System.out.println(Dog2.toString());
System.out.println(Dog3.toString());
}
}
谢谢, 杰克
答案 0 :(得分:1)
来自the docs for InputMismatchException:
由扫描程序抛出,表示检索到的令牌与预期类型的模式不匹配,或者令牌超出预期类型的范围。
这几乎说明了所有需要说的。检查文件是否有意外输入。