Hey guys so I'm newer to Java and was running through some exercises and I wrote the following code
package Java;
import java.util.*;
public class datatypespractice {
public static void main(String []args){
String[] dataTypes = {"* byte", "* short", "* int", "* long"};
Scanner number = new Scanner(System.in);
int w=number.nextInt();
for(int t=0; t<w; t++){
try{
long x = number.nextLong();
if (x <= 127 && x >= -128){
System.out.println(x + " can be fitted in:");
for(int i=0; i<=3; i++)
System.out.println(dataTypes[i]);}
else if (x <= 32767 && x >= -32768){
System.out.println(x + " can be fitted in:");
for(int i=1; i<=3; i++)
System.out.println(dataTypes[i]);}
else if (x <= 2147483647 && x >= -2147483648){
System.out.println(x + " can be fitted in:");
for(int i=2; i<=3; i++)
System.out.println(dataTypes[i]);}
else if (x <= 9223372036854775807L && x >=-9223372036854775808L ){
System.out.println(x + " can be fitted in:");
for(int i=3; i<=3; i++)
System.out.println(dataTypes[i]);}
} //closing try
catch(Exception x){
System.out.println(number.next() + " can't be fitted anywhere.");}
} //closing the for-loop
number.close();
} //closing main
} //closing class
After the catch exception at the bottom if I use the code
System.out.println(x + "can't be fitted anywhere.");
it will repeat the "can't be fitted anymore" statement until it runs out of the "for" repetitions.
However by replacing x with number.next() it only repeats once and allows me to continue with further inputs. Can you explain why this is?