0

I have imported java.util.* but the IDE can't recognize Scanner. If I change it to import java.util.Scanner; it's fine. But I need it on java.util.* because the catch exception is part of java.util. This code is from a textbook by the way.

EDIT: I'm using Eclipse.

import java.util.*;

public class GetInteger {

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.print("Enter an integer: ");
        int i = GetAnInteger();
        System.out.println("You entered " + i);
    }

    public static int GetAnInteger() {
        while(true) {
            try {
                return sc.nextInt();
            }
            catch (InputMismatchException e) {
                sc.next();
                System.out.println("That's not an integer. Try again: ");
            }
        }
    }
}

0 Answers0