I use the Scanner class and not BufferedReader + inputStream. It doesn't really matter what you use. I just prefer it due to easier to construct and it doesn't throw exceptions I must catch.
You may have noticed you get a NumberFormatException if you
do type in a non-integer number.
You can easily catch the exception and try again if it errored.
Scanner scanner [COLOR=#339933]=[/COLOR] [COLOR=#000000][B]new[/B][/COLOR] Scanner[COLOR=#009900]([/COLOR][COLOR=#003399]System[/COLOR].[COLOR=#006633]in[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000066][B]int[/B][/COLOR] number [COLOR=#339933]=[/COLOR] [COLOR=#339933]-[/COLOR][COLOR=#CC66CC]1[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000066][B]boolean[/B][/COLOR] ok[COLOR=#339933]=[/COLOR] [COLOR=#000066][B]false[/B][/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000000][B]while[/B][/COLOR] [COLOR=#009900]([/COLOR][COLOR=#339933]![/COLOR]ok[COLOR=#009900])[/COLOR][COLOR=#009900]{[/COLOR]
[COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]print[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Enter number: "[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#003399]String[/COLOR] dec [COLOR=#339933]=[/COLOR] scanner.[COLOR=#006633]nextLine[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000000][B]try[/B][/COLOR] [COLOR=#009900]{
[/COLOR] number [COLOR=#339933]=[/COLOR][COLOR=#003399]Integer[/COLOR].[COLOR=#006633]parseInt[/COLOR][COLOR=#009900]([/COLOR]dec[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] ok [COLOR=#339933]=[/COLOR] [COLOR=#000066][B]true[/B][/COLOR][COLOR=#339933]; [/COLOR][COLOR=#ff0000]//This line is skipped if parseInt(..) fails.
[/COLOR] [COLOR=#009900]}[/COLOR] [COLOR=#000000][B]catch[/B][/COLOR] [COLOR=#009900]([/COLOR][COLOR=#003399]NumberFormatException[/COLOR] e[COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR][COLOR=#a9a9a9]/*Do nothing with exception*/[/COLOR] [COLOR=#009900]}
[/COLOR] [COLOR=#009900]}
[/COLOR] [COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]println[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Correct number: "[/COLOR] [COLOR=#339933]+[/COLOR] number[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
This is - however-
the quick and dirty way. Making the exception object is expensive. In addition this just looks ugly.
A cleaner way is to test the String before trying to parse it to an int.
This can be done by simply looping over the characters and verifying it's a digit.
When checking a simple character, don't be tempted to write things like this:
[COLOR=#000066][B]char[/B][/COLOR] c [COLOR=#339933]=[/COLOR] [COLOR=#0000FF]'5'[/COLOR][COLOR=#339933];
[/COLOR][COLOR=#000000][B]if[/B][/COLOR][COLOR=#009900]([/COLOR]c[COLOR=#339933]>=[/COLOR][COLOR=#0000FF]'0'[/COLOR] [COLOR=#339933]&&[/COLOR] c[COLOR=#339933]<=[/COLOR][COLOR=#0000FF]'9'[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{
[/COLOR] [COLOR=#666666][I]//isdigit
[/I][/COLOR][COLOR=#009900]}[/COLOR]
The Character class has various methods like "isLetter", "isJavaLetter", "isLowerCase", "isUpperCase",... and obviously "isDigit".
Using the Character's class methods is better since, it should also validate weird signs, like if the program is run in China, and they got some weird symbol for a digit. The
if(c>='0' && c<='9') will fail, but Character.isDigit© should pass.
AAaaaanyway, just side-information. On with the String checking:
Scanner scanner [COLOR=#339933]=[/COLOR] [COLOR=#000000][B]new[/B][/COLOR] Scanner[COLOR=#009900]([/COLOR][COLOR=#003399]System[/COLOR].[COLOR=#006633]in[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000066][B]boolean[/B][/COLOR] ok[COLOR=#339933];
[/COLOR] [COLOR=#003399]String[/COLOR] dec[COLOR=#339933];
[/COLOR]
[COLOR=#000000][B]do[/B][/COLOR] [COLOR=#009900]{
[/COLOR] [COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]print[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Enter number: "[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] dec [COLOR=#339933]=[/COLOR] scanner.[COLOR=#006633]nextLine[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] ok [COLOR=#339933]=[/COLOR] [COLOR=#000066][B]true[/B][/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000000][B]for[/B][/COLOR] [COLOR=#009900]([/COLOR][COLOR=#000066][B]char[/B][/COLOR] c [COLOR=#339933]:[/COLOR] dec.[COLOR=#006633]toCharArray[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#009900]{
[/COLOR] [COLOR=#000000][B]if[/B][/COLOR] [COLOR=#009900]([/COLOR][COLOR=#339933]![/COLOR][COLOR=#003399]Character[/COLOR].[COLOR=#006633]isDigit[/COLOR][COLOR=#009900]([/COLOR]c[COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#009900]{
[/COLOR] ok [COLOR=#339933]=[/COLOR] [COLOR=#000066][B]false[/B][/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#009900]}
[/COLOR] [COLOR=#009900]}
[/COLOR] [COLOR=#009900]}[/COLOR] [COLOR=#000000][B]while[/B][/COLOR] [COLOR=#009900]([/COLOR][COLOR=#339933]![/COLOR]ok[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR]
[COLOR=#000066][B]int[/B][/COLOR] number [COLOR=#339933]=[/COLOR] [COLOR=#003399]Integer[/COLOR].[COLOR=#006633]parseInt[/COLOR][COLOR=#009900]([/COLOR]dec[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]println[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Correct number: "[/COLOR] [COLOR=#339933]+[/COLOR] number[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
I don't know how familiar you are with this type of loop yet, if you don't quite get it, it you'll learn it later and here's an equivalent.
Scanner scanner [COLOR=#339933]=[/COLOR] [COLOR=#000000][B]new[/B][/COLOR] Scanner[COLOR=#009900]([/COLOR][COLOR=#003399]System[/COLOR].[COLOR=#006633]in[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000066][B]boolean[/B][/COLOR] ok[COLOR=#339933];
[/COLOR] [COLOR=#003399]String[/COLOR] dec[COLOR=#339933];
[/COLOR] [COLOR=#000000][B]do[/B][/COLOR] [COLOR=#009900]{
[/COLOR] [COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]print[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Enter number: "[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] dec [COLOR=#339933]=[/COLOR] scanner.[COLOR=#006633]nextLine[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] ok [COLOR=#339933]=[/COLOR] [COLOR=#000066][B]true[/B][/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000066][B]int[/B][/COLOR] i[COLOR=#339933]=[/COLOR][COLOR=#CC66CC]0[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#000000][B]while[/B][/COLOR][COLOR=#009900]([/COLOR]i[COLOR=#339933]<[/COLOR]dec.[COLOR=#006633]length[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{
[/COLOR] [COLOR=#000066][B]char[/B][/COLOR] c [COLOR=#339933]=[/COLOR] dec.[COLOR=#006633]charAt[/COLOR][COLOR=#009900]([/COLOR]i[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000000][B]if[/B][/COLOR][COLOR=#009900]([/COLOR][COLOR=#339933]![/COLOR][COLOR=#003399]Character[/COLOR].[COLOR=#006633]isDigit[/COLOR][COLOR=#009900]([/COLOR]c[COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{
[/COLOR] ok [COLOR=#339933]=[/COLOR] [COLOR=#000066][B]false[/B][/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#666666][I]//You can 'break' after this, if you know the break command already. Don't bother if you don't.
[/I][/COLOR] [COLOR=#009900]}[/COLOR]
i[COLOR=#339933]=[/COLOR] i [COLOR=#339933]+[/COLOR] [COLOR=#CC66CC]1[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// or i+=1 or i++;
[/I][/COLOR] [COLOR=#009900]}
[/COLOR] [COLOR=#009900]}[/COLOR] [COLOR=#000000][B]while[/B][/COLOR] [COLOR=#009900]([/COLOR][COLOR=#339933]![/COLOR]ok[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000066][B]int[/B][/COLOR] number [COLOR=#339933]=[/COLOR] [COLOR=#003399]Integer[/COLOR].[COLOR=#006633]parseInt[/COLOR][COLOR=#009900]([/COLOR]dec[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]println[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Correct number: "[/COLOR] [COLOR=#339933]+[/COLOR] number[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
This is better than the try{}catch(){} simply because it's not there.
But it still looks quite lengthy to me. The next best option is knowing regular expressions.
In regular expressions "\d" stands for 'digit', and "+" stands for '1 or more'. Which means that when a String matches the regular expression "\d+" the String contains only digits. (1 or more digits)
Strings got a 'matches(..)' method, so it's easy. You just have to know regular expressions for it... And escape the backslash with another one as a backslash is 'special' for Java
Scanner scanner [COLOR=#339933]=[/COLOR] [COLOR=#000000][B]new[/B][/COLOR] Scanner[COLOR=#009900]([/COLOR][COLOR=#003399]System[/COLOR].[COLOR=#006633]in[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#003399]String[/COLOR] num[COLOR=#339933];
[/COLOR] [COLOR=#000000][B]do[/B][/COLOR] [COLOR=#009900]{
[/COLOR] [COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]print[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Enter number: "[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] num [COLOR=#339933]=[/COLOR] scanner.[COLOR=#006633]nextLine[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#009900]}[/COLOR] [COLOR=#000000][B]while[/B][/COLOR] [COLOR=#009900]([/COLOR][COLOR=#339933]![/COLOR]num.[COLOR=#006633]matches[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"[COLOR=#000099][B]\\[/B][/COLOR]d+"[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000066][B]int[/B][/COLOR] number [COLOR=#339933]=[/COLOR] [COLOR=#003399]Integer[/COLOR].[COLOR=#006633]parseInt[/COLOR][COLOR=#009900]([/COLOR]num[COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]println[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Correct number: "[/COLOR] [COLOR=#339933]+[/COLOR] number[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
And finally - This is Scanner specific - the Scanner class has methods to check if what you're going to read is an int or not:
[FONT=monospace]Scanner scanner [COLOR=#339933]=[/COLOR] [COLOR=#000000][B]new[/B][/COLOR] Scanner[COLOR=#009900]([/COLOR][COLOR=#003399]System[/COLOR].[COLOR=#006633]in[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR][/FONT]
[COLOR=#000066][B]int[/B][/COLOR] number [COLOR=#339933]=[/COLOR][COLOR=#003399]Integer[/COLOR].[COLOR=#006633]MIN_VALUE[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000000][B]do[/B][/COLOR][COLOR=#009900]{
[/COLOR] [COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]print[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Enter number: "[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#000000][B]if[/B][/COLOR][COLOR=#009900]([/COLOR]scanner.[COLOR=#006633]hasNextInt[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{
[/COLOR] number [COLOR=#339933]=[/COLOR] scanner.[COLOR=#006633]nextInt[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#009900]}[/COLOR] [COLOR=#000000][B]else[/B][/COLOR] [COLOR=#009900]{
[/COLOR] [COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]println[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Fool! You entered <"[/COLOR] [COLOR=#339933]+[/COLOR] scanner.[COLOR=#006633]nextLine[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000FF]">. That ain't no number."[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#009900]}
[/COLOR] [COLOR=#009900]}[/COLOR][COLOR=#000000][B]while[/B][/COLOR][COLOR=#009900]([/COLOR]number [COLOR=#339933]==[/COLOR] [COLOR=#003399]Integer[/COLOR].[COLOR=#006633]MIN_VALUE[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];
[/COLOR] [COLOR=#003399]System[/COLOR].[COLOR=#006633]out[/COLOR].[COLOR=#006633]println[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000FF]"Correct number: "[/COLOR] [COLOR=#339933]+[/COLOR] number[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
The downside is, you still have to get rid of the entered input by doing scanner.nextLine() if the input was no int. (Happens when I say "You fool! ...." )
And also the fact that you loop when number == Integer.MIN_VALUE. It just has to be a number which you don't expect the user to be typing. Integer.MIN_VALUE and MAX_VALUE are always good options for that ^^.
Even tho it's still safer with an extra boolean...
...Yup I felt like typing today :D