我很好奇 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());
}
}
谢谢,杰克