Hi, i have a question. we have this project for school and i have to check if an integer is a number. everywhere on the internet i find how to check if the input is an integer... But in this case the input is an integer, and on the internet the input is a String... plz help?
this is my code:
System.out.println("Kleur rood: 1");
System.out.println("Kleur blauw: 2");
System.out.println("Kleur groen: 3");
System.out.println("Kleur geel: 4");
int kleurtje = s.nextInt();
sow now i have to check if "kleurtje" really is an integer.
Check if integer is an integer
Page 1 of 14 Replies - 731 Views - Last Post: 18 April 2010 - 02:05 AM
Replies To: Check if integer is an integer
#2
Re: Check if integer is an integer
Posted 17 April 2010 - 06:11 AM
nextInt() will throw an exception if doesnt find an integer. So I guess your question really is how to handle if it's not an integer you are receiving. There are tree ways I can think of.
1. Handle the exception if you know how.
2. Use hasNextInt() before hand and only if true use nextInt(). That way "kleurtje" will definitely be an integer.
3. Use next() and receive a String which you then check if it can be interpreted as an integer and take appropriate action.
1. Handle the exception if you know how.
2. Use hasNextInt() before hand and only if true use nextInt(). That way "kleurtje" will definitely be an integer.
3. Use next() and receive a String which you then check if it can be interpreted as an integer and take appropriate action.
This post has been edited by LaFayette: 17 April 2010 - 06:12 AM
#3
Re: Check if integer is an integer
Posted 17 April 2010 - 06:30 AM
Read a string in, and use Integer.parseInt(String S)
It'll throw an exception if it isn't a string.
Orrrrrrrrr... You could use this
http://www.java2s.co...ceofKeyword.htm
the instanceOf operator.
It'll throw an exception if it isn't a string.
Orrrrrrrrr... You could use this
http://www.java2s.co...ceofKeyword.htm
the instanceOf operator.
This post has been edited by Ghlavac: 17 April 2010 - 06:48 AM
#4
Re: Check if integer is an integer
Posted 17 April 2010 - 07:01 AM
You can also just read a whole line line of text and use the Character class's isDigit(char) method to determine if every character entered is a digit.
use the input.nextLine() method for this manner of doing things
use the input.nextLine() method for this manner of doing things
String inputStr = input.nextLine();
...
public boolean isDigit(String str) {
for (int i = 0; i < str.length(); i++) {
if (!isDigit(str.charAt(i)))
return false;
return true;
}
#5
Re: Check if integer is an integer
Posted 18 April 2010 - 02:05 AM
Ok, thx for al the helpful info.
I used the parseInt, and had to make a local String.
I used the parseInt, and had to make a local String.
boolean Loop = true;
do {
System.out.println("Kleur rood: 1");
System.out.println("Kleur blauw: 2");
System.out.println("Kleur groen: 3");
System.out.println("Kleur geel: 4");
String x = s.next();
try{
kleurtje = Integer.parseInt(x);
} catch(NumberFormatException e){
System.out.println("Geef een geldig getal van 1 tot 4");
}
if (kleurtje >= 1 && kleurtje <= 4) {
Loop = false;
}
else {
System.out.println("Geef een getal tussen 1 & 4\n");
}
} while (Loop);
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|