Trouble with for loop continuing after catching an

2019-02-26 00:20发布

问题:

Hi I am semi new at java and cant figure this one out. After catching an exception I want a for loop to continue and keep reading for new integers. This was a challenge put online that wants you to take 5 (this telling how many inputs it should expect after),

-150,
150000,
1500000000,
213333333333333333333333333333333333,
-100000000000000.

And turning into this output:

-150 can be fitted in:
* short
* int
* long
150000 can be fitted in:
* int
* long
1500000000 can be fitted in:
* int
* long
213333333333333333333333333333333333 can't be fitted anywhere.
-100000000000000 can be fitted in:
* long

I want the computer to check if a number will not be to large for a byte, short, int, and long. It works (maybe not in the best way) until it reaches 213333333333333333333333333333333333. It causes the InputMismatchException (bc its to large) and the code catches it but after it doesn't work. Here is the output:

 -150 can be fitted in:
 * short
 * int
 * long
 150000 can be fitted in:
 * int
 * long
 1500000000 can be fitted in:
 * int
 * long
 0 can't be fitted anywhere.
 0 can't be fitted anywhere.

I really cant figure it out any help would be appreciated!

public static void main(String[] args) {
    int numofinput = 0;
    Scanner scan = new Scanner(System.in);
    numofinput = scan.nextInt();
    int[] input;
    input = new int[numofinput];
    int i =0;

    for(i = i; i < numofinput && scan.hasNext(); i++){ 
        try {

            input[i] = scan.nextInt();
            System.out.println(input[i] + " can be fitted in:");

            if(input[i] >=-127 && input[i] <=127){
                System.out.println("* byte");
            }if((input[i] >=-32768) && (input[i] <=32767)){
                System.out.println("* short");
            }if((input[i] >=-2147483648) && (input[i] <=2147483647)){
                System.out.println("* int");
            }if((input[i] >=-9223372036854775808L) && (input[i] <=9223372036854775807L)){
                System.out.println("* long");
            }

        }catch (InputMismatchException e) {
            System.out.println(input[i] + " can't be fitted anywhere.");
        }   
    }
}

回答1:

The problem is that the mismatched input remains unclaimed in the Scanner after the exception, so you are going to catch the same exception in the loop forever.

To fix this problem, your program needs to remove some input from Scanner, for example by calling nextLine() in the catch block:

try {
    ...
} catch (InputMismatchException e) {
    // Use scan.next() to remove data from the Scanner,
    // and print it as part of error message:
    System.out.println(scan.next() + " can't be fitted anywhere.");
}

The input[] array can be replaced with a single long input, because you never use data from prior iterations; hence, there is no need to store it in the array.

In addition, you should replace the call to nextInt with a call to nextLong, otherwise you are not going to process large numbers correctly.

You should also remove the condition for longaltogether

if((input[i] >=-9223372036854775808L) && (input[i] <=9223372036854775807L))

because it is guaranteed to be true given that reading nextLong has completed successfully.

Finally, using "magic numbers" in your program should be avoided in favor of pre-defined constants from the corresponding built-in Java classes, for example

if((input[i] >= Integer.MIN_VALUE) && (input[i] <= Integer.MAX_VALUE))

instead of

if((input[i] >=-2147483648) && (input[i] <=2147483647))