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