3D - 2

Help with run error: Exception in thread "main" java.util.InputMismatchException?

I have a program that will compile, but when I try to run it I get this error: run: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor (Scanner.java:840) at java.util.Scanner.next (Scanner.java:1461) at java.util.Scanner.nextInt (Scanner.java:2091) at java.util.Scanner.nextInt (Scanner.java:2050) at testScores1.calculateAverage(testScores1.java:68) at testScores1.main(testScores1.java:41) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds) I cannot for the life of me figure out how to fix it. Here is my code for the program I've marked the lines that it says the errors are on: import java.io.*; import java.util.*; public class testScores1 { public static void main(String[] args) throws FileNotFoundException { String lastName; int test1, test2, test3, test4, test5; DoubleClass avg = new DoubleClass(); char grade = 0; double classAvg = 0; int totalStudents = 0; Scanner inFile = new Scanner(new FileReader("test score.txt")); PrintWriter outfile = new PrintWriter("class grades.txt"); outfile.println("Student Test1 Test2 Test3 Test4 Test5 Average Grade"); while(inFile.hasNext()) { lastName = inFile.next(); test1 = inFile.nextInt(); test2 = inFile.nextInt(); test3 = inFile.nextInt(); test4 = inFile.nextInt(); test5 = inFile.nextInt(); calculateAverage(inFile, avg); //run erroe classAvg = classAvg + avg.getNum(); totalStudents++; calculateGrade(avg, grade); outfile.printf(lastName+ "%14d%4d%4d%4d%4d%5.2f%7c%n", test1,test2,test3,test4,test5,avg.getNum(),grade); } outfile.println("\n"); outfile.println("Class Average = " + (classAvg / totalStudents)); inFile.close(); outfile.close(); } public static void calculateAverage(Scanner inFile, DoubleClass avg) { int sum; int i; sum = 0; i = 1; while (i <= 5) { sum = sum + inFile.nextInt(); //run erroe i++; } avg.setNum(sum/i); } public static char calculateGrade(DoubleClass avg, char grade) { if (avg.getNum() >= 90) grade = 'A'; else if (avg.getNum() >= 80) grade = 'B'; else if (avg.getNum() >= 70) grade = 'C'; else if (avg.getNum() >= 60) grade = 'D'; else grade = 'F'; return grade; } }

Public Comments

  1. It must be alpha-numeric or decimal format. See Scanner#nextFloat.
  2. Your input file format: String int1 int2 int3 int4 int5 In calculateAverage() you need to read String first then the integers. Good luck.
Powered by Yahoo! Answers