Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links


Determine a whole number in java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 25-02-2010
Member
 
Join Date: Dec 2009
Posts: 192
Determine a whole number in java

Sponsored Links
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.

Reply With Quote
  #2  
Old 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());
Hope this will help you in your program and you will get the desired output.
Reply With Quote
  #3  
Old 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.
Reply With Quote
  #4  
Old 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");
Reply With Quote
  #5  
Old 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");
        }
Reply With Quote
  #6  
Old 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);
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Determine a whole number in java"
Thread Thread Starter Forum Replies Last Post
How to determine the model number of my Toshiba satellite pro T455 laptop? Naotau Portable Devices 4 27-07-2011 08:13 AM
How can we determine which application meets Java or DotNET eligibility criteria NathanDS Software Development 5 16-10-2010 02:45 PM
How to format a number in java? kamina23 Software Development 4 20-01-2010 08:01 PM
Determine maximum number of recent documents in Windows Server 2008 Computer_Freak Tips & Tweaks 1 28-07-2009 05:52 PM
Determine number of columns in a file Chrisch Software Development 3 12-05-2009 11:19 PM


All times are GMT +5.5. The time now is 12:15 PM.