I am a first year I.T student. We haven't really been taught PROPERLY about throws Exceptions and FileReaders. So I need help, why my program keeps telling me I have a InputMismatchException? Netbeans doesn't show any errors in my codes. and i just added a Method for Lowest Grade[since that's what our prof told us to do, he won't help us either]. Please Help. :(
import java.io.*;
import java.util.*;
public class JavaQ {
static final int noOfStudents = 16;
public static void main(String[] args)throws FileNotFoundException {
String[]students = new String[noOfStudents];
int [] scoreList = new int [noOfStudents];
double classAvg;
int classSize;
Scanner infile = new Scanner(new FileReader ("StudentGrades.txt"));
System.out.println("Processing Data");
classSize = getData(infile, students, scoreList);
System.out.println("Class size: " + classSize);
classAvg = (double)(sumScores(scoreList, classSize))/classSize;
printResult(students, scoreList, classAvg, classSize);
System.out.println();
}
public static int getData(Scanner inputFile, String[] s, int[] sList)
{
int size = 0;
String inputString;
while(inputFile.hasNext() && size<noOfStudents)
{
s[size] = inputFile.next();
sList[size] = inputFile.nextInt();
size++;
}
return size;
}
public static int sumScores(int list[], int clSize)
{
int sum = 0;
for(int i = 0; i < clSize; i++)
sum = sum + list[i];
return sum;
}
public static int maxIndex(int[] list, int clSize)
{
int hInd = 0;
for(int i = 1; i < clSize; i++)
if(list[i] > list [hInd])
hInd = i;
return hInd;
}
public static int minIndex(int[] list, int clSize)
{
int lInd = 0;
for(int i = 1; i < clSize; i++)
if(list[i] < list [lInd])
lInd = i;
return lInd;
}
public static void printResult(String[] s, int[] sList, double avg, int clSize)
{
int i;
int maxScore;
maxScore = sList[maxIndex(sList, clSize)];
System.out.println("Highest Score: " + maxScore);
System.out.print("Student whose score is hightest in the class: ");
for(i = 0; i < clSize; i++)
if (sList[i] == maxScore)
System.out.print(s[i] + " " );
System.out.println();
System.out.printf("Class Average:%.3f%n", avg);
System.out.println("Student whose scores is less than the class average: ");
for(i=0; i<clSize; i++)
if(sList[i] < avg)
System.out.println(s[i] + " your score is below class average.");
System.out.println();
}
}