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();
    }
}

Where is the exception happening? Post the full text of the error message because it has the line number.

The exception says that the user entered the wrong kind of data. Your code should put the input statement within a try{}catch block to catch the exception and tell the user the input was incorrect and ask the user to reenter the input. All this needs to be inside of a loop that continues looping until the user enters good data.

This is the error it shows. >.< I really have no Idea. :/

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at javaq.JavaQ.getData(JavaQ.java:41)
at javaq.JavaQ.main(JavaQ.java:24)
Java Result: 1

StudentGrades.txt:
Carlo 45
Peter 95
Juan 87
Miggy 88
Paul 45
Christian 65
Marie 65
Julie 63
Sara 30
Anna 84
Dennis 90
Janice 98
Dennis Jan 87
Marcel 78
Kukay 81
Juana 82

at java.util.Scanner.nextInt(Scanner.java:2119)
at javaq.JavaQ.getData(JavaQ.java:41)

At line 41 in JavaQ the code calls the Scanner class's nextInt() method.
The data that is to be read is not a number and the code throws the exception.

What data was the code trying to read as a number? Add some printlns to the code to print out everything as it is read so you can see where the problem is. You should be able to look at the print out and the file and see what the program was trying to read that causes the problem. The last data printed out should be just before the bad data.

At line 41 in JavaQ the code calls the Scanner class's nextInt() method.
The data that is to be read is not a number and the code throws the exception.

What data was the code trying to read as a number? Add some printlns to the code to print out everything as it is read so you can see where the problem is. You should be able to look at the print out and the file and see what the program was trying to read that causes the problem. The last data printed out should be just before the bad data.

I "think" the data that needs to be read as a number is the number of the size of the noOfStudents in the in the infile?

I'm really sorry, This really confusing and depressing for me. I can't get this program at all. :( I seriously need help alot on programming. >.< Thanks for the help for reading the error on Line41! I got to understand that part.

What was printed out when you added the printlns I suggested?
What were the last things printed out?

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

What was printed out when you added the printlns I suggested?
What were the last things printed out?

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

could you please give me an example of how I'll put a println?
Should I put it inside the method or main?

System.out.println("var=" + var);

Replace var with the variables used in your program

Put them just after where you read into variables using the next and nextInt methods.
Read the data
print the value of the data

System.out.println("var=" + var);

Replace var with the variables used in your program

Put them just after where you read into variables using the next and nextInt methods.
Read the data
print the value of the data

Its my getData method. Something is wrong with it. :/

Something is wrong with it

Please explain what problem you see.

Have you added the printlns to show what was read and where the problem in the input is?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.