Java 从文件扫描时输入不匹配错误
我试图从一个.txt文件中读取数据到我的程序中,它抛出了输入不匹配错误,尽管它看起来不应该这样做 错误Java 从文件扫描时输入不匹配错误,java,inputmismatchexception,Java,Inputmismatchexception,我试图从一个.txt文件中读取数据到我的程序中,它抛出了输入不匹配错误,尽管它看起来不应该这样做 错误 **********HELLO*********** Which animal are you looking to check into the kennel?: Dog Cat cat //typed entry Using file cats.txt Exception in thread "main" java.util.InputMismatchException at java.
**********HELLO***********
Which animal are you looking to check into the kennel?:
Dog
Cat
cat //typed entry
Using file cats.txt
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at KennelDemo.initialise(KennelDemo.java:82)
at KennelDemo.main(KennelDemo.java:337)
// The error occurs on this line:
infile.nextInt();
int feedsPerDay = infile.nextInt();
下面是代码片段:
public class KennelDemo {
private String filename; // holds the name of the file
private Kennel kennel; // holds the kennel
private Scanner scan; // so we can read from keyboard
private String tempFileName;
private String dogsFile = "dogs.txt";
private String catsFile = "cats.txt";
/*
* Notice how we can make this private, since we only call from main which
* is in this class. We don't want this class to be used by any other class.
*/
private KennelDemo() {
scan = new Scanner(System.in);
boolean fileCorrect = false;
do {
System.out.print("Which animal are you looking to check into the kennel?: " + "\n");
System.out.println("Dog");
System.out.println("Cat");
tempFileName = scan.next();
if(tempFileName.toLowerCase().equals("dog") || tempFileName.toLowerCase().equals("cat")) {
filename = tempFileName.toLowerCase().equals("dog") ? dogsFile : catsFile;
fileCorrect = true;
}
else {
System.out.println("That is not a valid filename, please enter either 'Dog' or 'cat' in lowercase.");
}
}
while(!fileCorrect);
}
/*
* initialise() method runs from the main and reads from a file
*/
private void initialise() {
kennel = new Kennel();
System.out.println("Using file " + filename);
// Using try-with-resource (see my slides from session 15)
try(FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
Scanner infile = new Scanner(br)){
String kennelName = infile.nextLine();
int kennelSize = infile.nextInt();
infile.nextLine();
kennel.setCapacity(kennelSize);
int numPets = infile.nextInt();
infile.nextLine();
kennel.setName(kennelName);
for(int i=0; i < numPets; i++){
String PetName = infile.nextLine();
int numOwners = infile.nextInt();
infile.nextLine();
ArrayList<Owner> owners = new ArrayList<>();
for(int oCount=0; oCount < numOwners; oCount++){
String name = infile.nextLine();
String phone = infile.nextLine();
Owner owner = new Owner(name, phone);
owners.add(owner);
}
//boolean mutualBoolean = infile.nextBoolean();
infile.nextLine();
String favFood = infile.nextLine();
infile.nextInt();
int feedsPerDay = infile.nextInt();
Pet Pet = new Pet(PetName, owners, favFood, feedsPerDay);
kennel.addPet(Pet);
}
} catch (FileNotFoundException e) {
System.err.println("The file: " + " does not exist. Assuming first use and an empty file." +
" If this is not the first use then have you accidentally deleted the file?");
} catch (IOException e) {
System.err.println("An unexpected error occurred when trying to open the file " + filename);
System.err.println(e.getMessage());
}
}
我花了好几个小时试图弄明白这一点(布尔值有问题,我最终只是从程序中删除了它,因为它让我非常恼火),但我几乎没有取得任何进展。我已经多次重写了.txt文件,以确保每个数据段都与下一步读取的内容相对应,但它仍然会抛出一个错误
提前感谢。当您调用
infle.nextLine()时
在第二个for循环中将所有者添加到列表后,您将跳过最喜爱的食物行,这意味着下一次调用nextInt将遇到一个字符串,而不是有效的int
。删除对nextLine()
的调用,您应该就可以了
//boolean mutualBoolean = infile.nextBoolean();
infile.nextLine();
String favFood = infile.nextLine();
infile.nextInt();
int feedsPerDay = infile.nextInt();
变成
//boolean mutualBoolean = infile.nextBoolean();
String favFood = infile.nextLine();
int feedsPerDay = infile.nextInt();
此外,在阅读喜爱的食物(参见上面的代码)后,您不需要调用infle.nextInt()
,因为您已经在正确的线路上了
最后,声明扫描仪实例所需的全部内容如下:
Scanner infile = new Scanner(new File(filename));
而不是
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
Scanner infile = new Scanner(br)
非常感谢你,这非常有效。我不能告诉你我有多头疼。将你的答案标记为已接受:)嗨,伙计。当我在.txt文件中有1只动物时,这是有效的,但一旦我有超过1只动物(因此文件中有更多的所有者,每个动物的所有者数量都有一个整数),它就会在你让我删除的行上抛出一个错误。如果我把它拿出来,我会得到和这篇文章一样的错误。有什么想法吗?
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
Scanner infile = new Scanner(br)