-
quick help
Code:
public static void main(String[] args) {
int numIntegers = 5;
int[] inputInts = new int[numIntegers];
Scanner scan = new Scanner(System.in);
for (int i = 0; i < numIntegers; i++) {
try {
inputInts[i] = scan.nextInt();
System.out.println("Got integer input: " + inputInts[i]);
} catch (InputMismatchException ime) {
System.out.println("Got non-integer input: " + scan.nextInt());
}
}
}
}
Can someone tell me whats wrong with this code?
-
Re: quick help
You're going to have to tell us. Does it compile? Does it run? Does it throw an Exception? Generate incorrect output? Something else?
Have you stepped through this with a debugger, or at least added some print statements, to figure out what's going on?
-
Re: quick help
add this :
Code:
public static void main(String[] args) {
final int numIntegers = 5; // need to be a const
int[] inputInts = new int[numIntegers];
Scanner scan = new Scanner(System.in);
for (int i = 0; i < numIntegers; i++) {
try {
System.out.print("input "+i+" : ");
inputInts[i] = scan.nextInt();
System.out.println("Got integer input: " + inputInts[i]);
} catch (InputMismatchException ime) {
System.out.println("Got non-integer input: " + scan.nextInt());
}
}
}
}
-
Re: quick help
Please go through the Forum Rules -- particularly the third paragraph.
db