java 代码中的 InputMismatchException 错误。请给些建议

InputMismatchException error in java code. Please give some advice

出于某种原因,我一直在第 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 ++;
            }
        }        

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

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

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

因此,理想情况下,您的代码应该是

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

此外,我之前尝试过读取csv文件,您也可以参考以下代码片段来读取分号分隔的文件。

        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);

希望对您有所帮助。