Hi All,
Having some difficulties with this section.
The program runs but is giving me some errors.
Not sure on how to fix these.
Any guidance would be greatly appreciated.
My code:
import java.io.*; import java.util.*; public class Q4 { public static void main(String[] args) //Throw any exceptions throws Exception { Scanner input = new Scanner(new File("Salary.txt")); String assistant = "assistant"; String associate = "associate"; String full = "full"; double assistantTotal = 0.0; double associateTotal = 0.0; double fullTotal = 0.0; double allFacultyTotal = 0.0; double assistantAverage = 0.0; double associateAverage = 0.0; double fullAverage = 0.0; double allFacultyAverage = 0.0; int assistantCounter = 0; int associateCounter = 0; int fullCounter = 0; int allFacultyCounter = 0; while (input.hasNextLine()) { String line = input.nextLine(); if (line.toLowerCase().contains(assistant.toLowerCase())) { assistantCounter++; assistantTotal += input.nextDouble(); assistantAverage = assistantTotal / assistantCounter; allFacultyTotal += input.nextDouble(); allFacultyCounter++; } else if (line.toLowerCase().contains(associate.toLowerCase())) { associateCounter++; associateTotal += input.nextDouble(); associateAverage = associateTotal / associateCounter; allFacultyTotal += input.nextDouble(); allFacultyCounter++; } else if (line.toLowerCase().contains(full.toLowerCase())) { fullCounter++; fullTotal += input.nextDouble(); fullAverage = fullTotal / fullCounter; allFacultyTotal += input.nextDouble(); allFacultyCounter++; } } allFacultyAverage = allFacultyTotal / allFacultyCounter; System.out.println("The average pay for Assistants is: " + assistantAverage); System.out.println("The average pay for Associates is: " + associateAverage); System.out.println("The average pay for Full Professors is: " + fullAverage); System.out.println("The average pay all faculty members is: " + allFacultyAverage); System.out.println("The total pay for all Assistants is: " + assistantTotal); System.out.println("The total pay for all Associates is: " + associateTotal); System.out.println("The total pay for all Full Professors is: " + fullTotal); System.out.println("The total pay for all faculty members is: " + allFacultyTotal); input.close(); } }
I'm getting these errors:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Q4.main(Q4.java:38)
At line 38 is this: assistantTotal += input.nextDouble();
This is in the IF block.
Just for info, the file Salary.txt is comprised of many lines of the following format.
FirstName1 LastName1 assistant 79174.73
FirstName2 LastName2 associate 70817.75
FirstName3 LastName3 associate 69619.0
FirstName4 LastName4 full 116992.43