gpt4 book ai didi

java - 从文件扫描时输入不匹配错误

转载 作者:行者123 更新时间:2023-12-02 04:44:58 24 4
gpt4 key购买 nike

我正在尝试将 .txt 文件中的数据读入我的程序,但它抛出了 InputMismatchErrors,尽管看起来不应该。

错误

**********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 文件 (cats.txt)

DogsRUs
20
1
TestCat
1
TestOwner
12345
TestFood
3

我花了几个小时试图解决这个问题( boolean 值有问题,我最终完全从程序中取出来,因为它让我很烦),但我几乎没有取得任何进展。我已经多次重写 .txt 文件,以确保每条数据与接下来读取的数据相对应,但它仍然会引发错误。

提前致谢。

最佳答案

当您在第二个 for 循环中将所有者添加到列表后调用 infile.nextLine(); 时,您将跳过最喜欢的食物行,这意味着对 nextInt 的下一次调用将遇到一个String,而不是一个有效的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();

此外,在读入最喜欢的食物(参见上面的代码)后,您不需要调用 infile.nextInt(),因为您已经位于正确的行上。

最后,声明 Scanner 实例所需的全部内容如下:

Scanner infile = new Scanner(new File(filename));

而不是

FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
Scanner infile = new Scanner(br)

关于java - 从文件扫描时输入不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29736565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com