-1

I'm making simple program where the user inputs a class number of a student(cn) and the grade of that student(ngrade). I was trying to add an exception that checks if the input of the cn and ngrade are integers. If not the user will be informed that it is invalid and asks to re enter the details.

problem: i did use while loop to check the inputs throughout the loop. but i'm getting a infinite loop.

CODE:

import java.io.*;
import java.util.*;

public class trrying {

    public static void main(String[] args) {
        int ngrade;
        int cn;
        int A = 0, B = 0, C = 0, D = 0, E = 0;
        boolean test = false;
        Scanner sn = new Scanner(System.in);

        while (!test) {
            try {
                for (int i = 0; i <= 2; i++) {

                    System.out.println("Enter class number: ");
                    cn = sn.nextInt();
                    System.out.println("Enter numeric grade: ");
                    ngrade = sn.nextInt();
                    System.out.println("Letter Grade: ");

                    if (ngrade >= 90) {
                        A++;
                        System.out.println("A");
                    } else {
                        System.out.println("HELLO");
                    }

                    test = true;

                }
                test = false;
            } catch (Exception e) {
                System.out.println("ERROR! ");
                // System.out.println("Enter class number: ");

            }

        }
    }
}
weston
  • 51,132
  • 20
  • 132
  • 192
Zurc
  • 61
  • 5
  • Related: [Validating input using java.util.Scanner](http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner), [How to use Scanner to accept only valid int as input](http://stackoverflow.com/questions/2912817/how-to-use-scanner-to-accept-only-valid-int-as-input) – Pshemo Feb 19 '17 at 13:43

1 Answers1

-2

One option to get the behavior you want would be to read in each input as a string, using Scanner#nextLine(), and then manually try to parse that input as an integer. If successful, accept that input and continue with your logic. If not successful, then catch the exception, and repeat the loop to get another user input.

for (int i=0; i <= 2; i++) {
    String line = "";
    while (true) {
        System.out.println("Enter class number: ");
        line = sn.nextLine();
        try {
            cn = Integer.parseInt(line);
            break;
        } catch(Exception e) {
            System.out.println("Enter class number as an integer only.");
        }
    }
    while (true) {
        System.out.println("Enter numeric grade: ");
        line = sn.nextLine();
        try {
             ngrade = Integer.parseInt(line);
             break;
        } catch(Exception e) {
            System.out.println("Enter numeric grade as an integer only.");
        }
    }

    System.out.println("Letter Grade: ");

    if (ngrade >= 90) {
        System.out.println("A");
    }        
    else {
         System.out.println("HELLO");
    }

    test = true;
}
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263