I'm writing a little program for myself and I bump into a problem.
I want to be able to type a number into the console, and extract the number for a variable. Until this point I got it.
int aNumber = Integer.parseInt(br.readLine());
Except I want to get an error when there is something entered into the console that's not a number.
Something like this but working ofcourse:
void Test() {
while (true) {
try {
aNumber = Integer.parseInt(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
if (aNumber >= 1) {
System.out.println("Hurray you enterd a number! It is " + aNumber + ".");
return;
} else {
System.out.println("Error: Something went wrong. Please try again.\n");
}
}
}
If you enter a number it works fine. But I want to be able to enter texts and getting an error to try again.
Example if you type in the console:
1
Answer:
Hurray you enterd a number! It is 1.
=====
2564
Answer:
Hurray you enterd a number! It is 2564.
=====
eaf123
Answer:
Error: Something went wrong. Please try again.
=====
Thanks for everyone trying to help.
Lars Meeuwsen