Search code examples
javainputmismatchexception

InputMismatchException error in java code. Please give some advice


For some reason I keep getting the inputmistmatch exception on line 44 (int fileAge = inputFile.nextInt();)

I have tried isolating that part of the code and using it with a manually created file and it still gives the same error. I have tried using different delimiters instead of the semi colon. I tried removing the age variable and instead re doing the code so it only asks from the gender onwards but then I start getting a no such element exception. Not sure what the issue is.

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 ++;
            }
        }        

Solution

  • One of the issue which I can directly see is :

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

    The constructor of scanner class takes in a reference of java.io.File class, not a sting name of the file you want to read.

    So, you code should ideally be

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

    Also, I have tried previously to read the csv file, you can refer following code snippet to read a semicolon separated file also.

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

    Hope that helps you in some direction.