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.");
}
}
}