1

Sorry but I'm still very new to Java and I've tried to figure this out with online help. I am trying a try/catch to handle InputMismatchException after "Enter the homework grades for the students" (in case they enter a letter rather than number). It is not working out so far. What should the code look like to accomplish this?

package exceptionHandling;
import java.util.Scanner;
import java.util.InputMismatchException;

public class ExceptionHandling {

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    double total=0;     
    System.out.println("Enter the number of homework assignments:");
    int homeworkGrades = scan.nextInt();
    double hw[]=new double[homeworkGrades];

    System.out.println("Enter the homework grades for the student:");
    for (int hw2=0;hw2<hw.length;hw2++){
        hw[hw2]=scan.nextDouble();
    }
        for(int i=0;i<hw.length;i++){
            total=total+hw[i];
        }   

    scan.close();
    double average=total/homeworkGrades;
    System.out.println("The average homework grade is "+average);
    if (average < 101 && average >= 90) {
        System.out.println("A");
    }
        else if (average < 90 && average >= 80) {
            System.out.println("B");
        }
            else if (average < 80 && average >= 70) {
                System.out.println("C");
            }
            else if (average < 70 && average >= 60) {
                System.out.println("D");
            }
            else if (average < 60) {
                System.out.println("F");
        }
}

}

I still get the InputMismatchError even when I try and catch around the code for the "Enter the homework grades for the students". I attempt inserting the try and catch to different spots and use different code within it but no luck.

EDIT: No guys, duplicate scanner is not my issue. I cannot successfully handle InputMismatchException and I've been trying to do so for several hours. Please help!

StevieJ321
  • 25
  • 4
  • 1
    Possible duplicate of [Validating input using java.util.Scanner](https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Tom Aug 25 '19 at 22:37

3 Answers3

1

You could use .hasNextDouble(), .hasNextInt(), etc. to check if there is another double if you don't want to deal with the exception error.

0

You can change your code with: Old code: hw[hw2]=scan.nextDouble();

New code: hw[hw2]= Double.valueOf(scan.next().trim());

0

My suggestion is to scan input first as String then see if it can be phrased as int or double:

String test = "1234.0";
if (test.matches("[0-9.]+")) {
    double val = Double.parseDouble(test);
    System.out.println(val);
} else {
    System.out.println("not found");
}

test.matches("[0-9.]+") returns true if String test contains only digits and float point. Thus if there're other characters in test, the matches() will return a false.

The "[0-9.]+" is a regular expression, I'm not sure if your course allows it. If not, you might need to iterate through String characters and use isDigit() to make judgement.

Same can be applied for int after changing the regex to "[0-9]+", aka without float point. Then:

int val = Integer.parseInt(test);
Amarth Gûl
  • 913
  • 1
  • 12
  • 29