4 Replies - 907 Views - Last Post: 18 April 2010 - 02:05 AM Rate Topic: -----

#1 thalorre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#5 thalorre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1