# Check if integer is an integer

Page 1 of 1

## 4 Replies - 1419 Views - Last Post: 18 April 2010 - 02:05 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=168566&amp;s=42dbbf28c789d31d5bd5aff69ef8f832&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 thalorre

Reputation: 0
• Posts: 7
• Joined: 16-December 09

# Check if integer is an integer

Posted 17 April 2010 - 05:29 AM

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.

Is This A Good Question/Topic? 0

## Replies To: Check if integer is an integer

### #2 LaFayette

• D.I.C Regular

Reputation: 43
• Posts: 326
• Joined: 24-November 08

## 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.

This post has been edited by LaFayette: 17 April 2010 - 06:12 AM

### #3 Ghlavac

Reputation: 84
• Posts: 519
• Joined: 14-January 09

## 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.

This post has been edited by Ghlavac: 17 April 2010 - 06:48 AM

### #4 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## 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

```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 thalorre

Reputation: 0
• Posts: 7
• Joined: 16-December 09

## 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.
```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);

```