java - Input Mismatch Exception error -
i'm getting input mismatch error i'm not sure why. working on different computer seems not working on laptop.at first thought might txt file not reason.
import java.io.*; import java.util.*; public class findgrade { public static final int num_score_types = 5; public static void main(string[] args) { scanner scan = null; int[] quizarray = null; int[] labarray = null; int[] attendance = null; int[] midterms = null; int quizgrade =0; int labgrade=0; int attendance_1=0; int midterms_1 =0; string name; try { scan = new scanner(new file("input.txt")); } catch (filenotfoundexception e) { e.printstacktrace(); return; } // each iteration single exam type (ie: quizzes 1st one) (int = 0; < num_score_types; i++) { name = scan.next(); int numscores = scan.nextint(); int maxgrade = scan.nextint(); if (name.equals("quizzes")) { quizarray = new int[numscores]; readscores(quizarray, numscores, scan); } else if (name.equals("labs")) { labarray = new int[numscores]; readscores(labarray, numscores, scan); } else if (name.equals("lab_attendance")) { attendance = new int[numscores]; readscores(attendance, numscores, scan); } else if (name.equals("midterms")) { midterms = new int[numscores]; readscores(midterms, numscores, scan); } } } public static void readscores(int[] scorearray, int numscores, scanner scan) { (int = 0; < numscores; i++) { scorearray[i] = scan.nextint(); } } public static void average(double [] scorearray, int numscores){ double sum=0; for(int i=0; i< scorearray.length; i++){ sum += scorearray[i]; } double average = sum/numscores; system.out.println(sum + " " + average); }
}
output:
exception in thread "main" java.util.inputmismatchexception @ java.util.scanner.throwfor(scanner.java:840) @ java.util.scanner.next(scanner.java:1461) @ java.util.scanner.nextint(scanner.java:2091) @ java.util.scanner.nextint(scanner.java:2050) @ findgrade.main(findgrade.java:33)
input file:
quizzes 8 10 5 8 9 10 4 0 10 7 labs 6 100 95 90 100 87 63 92 lab_attendance 16 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 midterms 2 100 87 94 final 0 100
your code working fine data posted. give error if variable "name" assigned value different 5 names expecting, "quizze" instead of "quizzes". in case code logic gets corrupted , have error. 1 possible root cause.
Comments
Post a Comment