【问题标题】:InputMismatchException error in java code. Please give some advice [duplicate]Java 代码中的 InputMismatchException 错误。请给一些建议[重复]
【发布时间】:2019-10-20 04:49:08
【问题描述】:

出于某种原因,我在第 44 行不断收到 inputmistmatch 异常 (int fileAge = inputFile.nextInt();)

我已尝试隔离该部分代码并将其与手动创建的文件一起使用,但它仍然给出相同的错误。我尝试使用不同的分隔符而不是分号。我尝试删除年龄变量,而是重新编写代码,因此它只从性别开始询问,但随后我开始收到没有此类元素的异常。不知道是什么问题。

public static void main(String[]args) {
        Scanner kb = new Scanner (System.in);
        int age;
        String gender;
        String email;
        int salary;
        int end;
        int maleCount = 0;
        int femaleCount = 0;

        do {
        System.out.println("Please enter age");
        age = kb.nextInt();
        age = validateAge(age);
        System.out.println("Please enter gender. male or female: ");
        gender = kb.next();      
        System.out.println("Please enter email");
        email = kb.next();
        System.out.println("Please enter annual salary");
        salary = kb.nextInt();
        salary = validateSalary(salary);
        try {
            FileWriter fw = new FileWriter("salaries.txt", true);
            PrintWriter outputFile = new PrintWriter(fw);
            outputFile.println(age + ";" + gender + ";" + email + ";" + salary);
            outputFile.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
        System.out.println("Add another person or end here? Enter 1 to end or 2 to continue: ");
        end = kb.nextInt();
        }while (end != 1);

        Scanner inputFile = new Scanner("salaries.txt").useDelimiter(";");
        while(inputFile.hasNext()) {
            int fileAge = inputFile.nextInt(); //i get the error here
            String fileGender = inputFile.next();
            String fileEmail = inputFile.next();
            int fileSalary = inputFile.nextInt();
            if(fileGender.equals("male")) {
                maleCount ++;
            }else {
                femaleCount ++;
            }
        }        

【问题讨论】:

    标签: java inputmismatchexception


    【解决方案1】:

    我可以直接看到的一个问题是:

     Scanner inputFile = new Scanner("salaries.txt").useDelimiter(";");
    

    scanner 类的构造函数接受 java.io.File 类的引用,而不是您要读取的文件的字符串名称。

    所以,理想情况下,您的代码应该是

           File file = new File("salaries.txt");
            Scanner inputFile = new Scanner(file);
    

    另外,我之前尝试过读取csv文件,你也可以参考下面的代码sn-p来读取分号分隔的文件。

            File file = new File("salaries.txt");
            System.out.println(file.getAbsolutePath());
            while (inputFile.hasNextLine()) {
               String line = inputFile.nextLine();
               String[] lineArray = line.split(";");
               int fileAge = Integer.parseInt(lineArray[0]);
               String fileGender = lineArray[1];
               String fileEmail = lineArray[2];
               int fileSalary = Integer.parseInt(lineArray[3]);
               if (fileGender.equals("male")) {
                   maleCount++;
               } else {
                   femaleCount++;
                }
            }
            System.out.println(maleCount);
    
    

    希望对您有所帮助。

    【讨论】:

    • 哦,对了,我不敢相信我忘记了这个文件 file = new File("salaries.txt");扫描仪输入文件 = 新扫描仪(文件);这样做是为了现在我在 fileSalary 字段上得到错误,而不是在代码的前面。
    • 好的,您的解决方案允许我获得正确的输出,因此从技术上讲它可以解决整个问题。但我仍然不明白为什么我的方法会出现该错误。感谢您的帮助,我很感激。
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    相关资源
    最近更新 更多