InputFile不匹配异常

时间:2016-12-12 19:42:49

标签: java exception

我无法让我的代码读取名为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();

    }

}

相反,我在文本文件的第一行得到输入Mismatch异常。我正在读取一个String,一个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

1 个答案:

答案 0 :(得分:0)

作为快速解决方法,您可以代替package practise; public class code{ public static void main(String[] args){ int[] Array = {5,8,6,4}; int[] newArray = new int[Array.length]; int a, b, c, d, e, f =1; for(int z : Array ){ d=0; for(int i=0; i<Array.length; i++){ a = z; b = Array[i]; if( a >= b){ continue;} else{ d++;} } c = Math.subtractExact(Array.length , d); e = Math.subtractExact(c, f); for(int j=0; j< Array.length; j++){ while( j == e){ newArray[j] = z; break; } } } System.out.println("Here is your old Array :"); for(int k : Array){ System.out.println(k); System.out.println("Here is your new Bubble Sort Array :"); for(int q : newArray){ System.out.println(q); } } } } - &gt; fileReader.nextDouble()

我还没有弄清楚为什么会发生这种情况。我正在检查并将编辑我的答案。干杯!

稍后编辑:问题是由于Double.valueOf(fileReader.next())使用了错误的区域设置。要解决您的问题,请在创建Scanner Scanner之后完成所有操作。

工作样本:

fileReader.useLocale(Locale.US)

我的pets.txt文件就是你给出的一个例子。

希望它有所帮助!