【发布时间】:2016-12-12 19:42:48
【问题描述】:
我无法让我的代码读取名为 pets.txt 的文件。我检查了其他类似的问题,但似乎找不到解决方案。当我尝试读取这样的一行时会发生错误:
名称,1,1.2
这里的代码应该输出pets.txt中宠物的名字、年龄和体重
// import packages
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.FileReader;
public class Pets
{
// throw exception if the file isn't found
public static void main (String[] args) throws FileNotFoundException
{
// Create a scanner objcct to read the file
Scanner fileReader = new Scanner(new FileReader("pets.txt"));
// Initiate objects and variables
PetRecord firstPet = null;
PetRecord pet = null;
PetRecord smallestPet = null;
PetRecord largestPet = null;
PetRecord oldestPet = null;
PetRecord youngestPet = null;
double age = 0, weight = 0;
int counter = 0;
if(fileReader.hasNext())
{
// Create a reference to compare values
firstPet = new PetRecord();
// Create a delimiter to find the pet data
fileReader.useDelimiter(",");
// Call accessors to set name and age in the file
firstPet.setName(fileReader.next());
firstPet.setAge(fileReader.nextInt());
// Set a delimiter to read the next line
fileReader.useDelimiter("[,\n]");
// set the weight using weight accessor
firstPet.setWeight(fileReader.nextDouble());
// Print firstPet info
System.out.println(firstPet);
// Use pet1 to set values to be compared
smallestPet = firstPet;
largestPet = firstPet;
oldestPet = firstPet;
youngestPet = firstPet;
// Increment counter
counter++;
// Add firstPet to age and weight total
age += firstPet.getAge();
weight += firstPet.getWeight();
}
// A while loop to read until there are no more lines
while(fileReader.hasNext())
{
// Ctreate a new pet and read the data with the fileReader and delimiter
pet = new PetRecord();
pet.setName(fileReader.next());
fileReader.useDelimiter(",");
pet.setAge(fileReader.nextInt());
fileReader.useDelimiter("[,\n]");
pet.setWeight(fileReader.nextDouble());
// Print the pets info
System.out.println(pet);
// if statement to find largest and smallest pet
if(pet.getWeight() < smallestPet.getWeight())
smallestPet = pet;
if(pet.getWeight() > largestPet.getWeight())
largestPet = pet;
// if statment to find oldest and youngest
if(pet.getAge() > oldestPet.getAge())
oldestPet = pet;
if(pet.getAge() < youngestPet.getAge())
youngestPet = pet;
// demonstrate use of equals
if(pet.equals(firstPet))
System.out.println(pet + " is equal to " + firstPet);
// inctrement counter for each pet, keeping track of total
counter++;
// add each pets age and weight to the total
age += pet.getAge();
weight += pet.getWeight();
}
// divide the age and weight by the counter to find the average of each
age /= counter;
weight /= counter;
// Print the data
System.out.println();
System.out.println("Smallest Pet:\t" + smallestPet);
System.out.println("Largest Pet:\t" + largestPet);
System.out.println("Youngest Pet:\t" + youngestPet);
System.out.println("Oldest Pet:\t" + oldestPet);
System.out.println("Average Age:\t" + age + " years");
System.out.println("Average Weight:\t" + weight + "lb");
fileReader.close();
}
}
相反,我在文本文件的第一行收到输入不匹配异常。我正在读取一个字符串、一个 int,然后是一个 double,所以不确定不匹配发生在哪里....我仔细检查以确保文件保存在同一个项目文件夹中。
编辑:错误如下:
exception java.util.fileInputMismatch at:
java.util.Scanner.throwFor(Unknown Source)
java.util.Scanner.next(Unknown Source)
java.util.Scanner.nextDouble(Unknown Source)
at Pets.Main(Pets.java:37)
编辑:这里是 pets.txt
Barney,5,12.6
Frenchy,3,43.5
Marcus,4,54.2
Gilbert,12,4.6
Milley,2,7.4
【问题讨论】:
-
你在调用
nextDouble()时遇到不匹配的问题吗?