TechArena Community Determine a whole number in java

# Determine a whole number in java

## Software Development

#1
25-02-2010
 Member Join Date: Dec 2009 Posts: 192
Determine a whole number in java

Hello,
I really began in java and make a little program I would like to know how little test if the square root of a number is an integer or not. I would have the equivalent function is_int () in php
Code:
```int i;
if (whole?(Math.sqrt(i)))
{
...
}```
I would also like to retrieve the number of character to an integer (int)
but such i.length does not. How to determine whole numbers in java. Thanks in advance.

#2
25-02-2010
 Member Join Date: Nov 2009 Posts: 356
Re: Determine a whole number in java

Hello,
Try the following example
Code:
```float f = Math.sqrt(i);
if (Math.rint(f)== f) {
....
}```
for the entire test. If not for the length in characters:
Code:
```i =5588126;
System.out.System.out.println((""+ i).length());```
#3
25-02-2010
 Member Join Date: Nov 2009 Posts: 347
Re: using the math() function in java

Hello,
You can try the following code and you will get the whole numbers using java. Just have a look at it
Code:
```try{
int x =(int) Math.sqrt(i);
}catch (ClassCastException e){
System.out.System.out.println("This is not an integer");
}```
If you need any more help on this , then feel free to post back and we will find a solution for your query.
#4
25-02-2010
 Member Join Date: Nov 2009 Posts: 359
Re: Determine a whole number in java

Hello,
You can also try this example, it will give the same result but it is well formatted and well defined. If you like it then you can go with this code
Here is the code:
Code:
```double in = 26;
double y = Math.sqrt(in);
int x = (int)y;
if ( y% x! = 0 ) out.System.out.println("This is not an integer");```
#5
25-02-2010
 Member Join Date: Nov 2009 Posts: 446
Re: code for whole numbers

Hello,
The following code is more simpler that the others, try it and see how the output is given by the program
Code:
```        if (Math.rint(nb)== nb) {
System.out.System.out.println("Integer");
/ / processing
}
else{
System.out.System.out.println("No integer");
}```
#6
25-02-2010
 Member Join Date: Nov 2009 Posts: 518
Re: Determine a whole number in java

Hello,
This example gives you the same output using the different logic and even some of the different methods from java are used. Have a look at the code
Code:
```int a = 123857;
char[] b = Integer.function toString() {
[native code]
}(has).toCharArray();
char c[] = { b[b.length-1]};
String s = String.copyValueOf(c);
System.out.System.out.println(s);```