Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

testing for integer  RSS feed

 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you test if something is an integer value or not. For example for a variable a, how would you find out if it is an integer value or not.
So far I can only come up with the following test but can't figure out how to do it!!
ie, if(a != ...)
Please help me- I need to know to complete my assignment.
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Naf
I had to do a lot of home work regarding this. Finally I got the solution. And here is the code for it. If u require any futher details, I'll give u provided if u send ur mail ID.
import java.lang.*;
class Upper
{
private float val;
Upper(float a)
{
val=a;
}
float getIntVal()
{
return val;
}
boolean checkIntVariable()
{
if(getType(val).equals("int"))
return true;
else
return false;
}
String getType(int val)
{
return (((new Integer(val)).TYPE).getName());
}
String getType(float val)
{
return (((new Float(val)).TYPE).getName());
}
String getType(double val)
{
return (((new Double(val)).TYPE).getName());
}
String getType(boolean val)
{
return (((new Boolean(val)).TYPE).getName());
}
}
class TestIntegerProg
{
public static void main(String args[])
{
Upper u=new Upper(5);
System.out.println(u.checkIntVariable());
}
}
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naf Rash:
How do you test if something is an integer value or not. For example for a variable a, how would you find out if it is an integer value or not.
So far I can only come up with the following test but can't figure out how to do it!!
ie, if(a != ...)
Please help me- I need to know to complete my assignment.

Look at the api documentation for String's toCharArray method. Then look at the Character class's isDigit method.
Or look at parseInt method of Integer class.
 
Billybob Marshall
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naren Chivukula:
Hello Naf
I had to do a lot of home work regarding this. Finally I got the solution...

Um, val is a float, so it will always call the getType(float) method. That's just about the most convoluted non-solution I've ever seen.
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a simple solution, simply attempt to use the variable as an int, (in a try..catch) of course, and see
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you test if something is an integer value or not. For example for a variable a, how would you find out if it is an integer value or not.
What type is variable a in your example? If it's an int, then the answer is obvious
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quote:
-------------------------------------------------------------------------
Um, val is a float, so it will always call the getType(float) method. That's just about the most convoluted non-solution I've ever seen.
-------------------------------------------------------------------------
It should be noted that if val is of any type say, int/char/double/float/boolean, And your intension is to test val for int then the program works fine. I think the solution I gave is the right one.
Marshall should note this.
 
Billybob Marshall
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naren Chivukula:


Thus the convoluted, non-solution reply.
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quote:
code:
--------------------------------------------------------------------------------
class Upper{ private float val; // bbm - THIS IS ALWAYS A FLOAT boolean checkIntVariable() { if(getType(val).equals("int")) // bbm - THIS IS ALWAYS CALLING THE getType(float) METHOD return true; else return false; // bbm - SO IT ALWAYS RETURNS false } String getType(float val) { return (((new Float(val)).TYPE).getName()); }}class TestIntegerProg{ public static void main(String args[]) { Upper u=new Upper(5); System.out.println(u.checkIntVariable()); // bbm - WILL DISPLAY false }}
--------------------------------------------------------------------------------

Yeah, it always returns false 'cause Type of val is float. This is the solution that Naf really wants. Do refer the question posed, it is required to check whether a variable is int or not. If u place any variable instead of val. Then, if it is really a int type then returns true else false.
Marshall shouldn't be confused.
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming that a is a String, and you're trying to determine whether it can be converted to an int or not.
One way is just to try it (remember I'm assuming a is a String):

To me this seems highly inelegant, though, so I'd not use the exception litmus test. Instead, I'd use a regex:

If you need to be able to detect other encodings, like "0xCAFE" format for hex numbers or "0767" for octals, you could do those checks separately:

If this is not the case, and this is some sort of trick question your teacher wants you to answer only knowing that a is a primitive, that's not too hard either:

sev
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!