0

I am a Java newbie and I am facing this error here. Any help would be appreciated. Thank you!

The error is:

symbol: method isInteger(double)
location: class number

Program:

int num = Integer.parseInt(num1.getText()); 
double square = Math.sqrt(num);



if (Number.isInteger(square))
    outputlbl.setText("Number is a perfect square");
else 
    outputlbl.setText("Number is not a perfect square");
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • For different approaches to the perfect square problem see also: [Fastest way to determine if an integer's square root is an integer](https://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer?rq=1) – Federico klez Culloca Aug 30 '21 at 13:32
  • And since you're a newbie, a piece of advice: the fact that you *can* omit braces around the body of conditionals doesn't mean you *should*. – Federico klez Culloca Aug 30 '21 at 13:34
  • @FedericoklezCulloca ahh thank you so much, I used the modulo operator and it worked well. Im sorry, I couldn't really catch what you meant by omitting braces around the conditionals ;-; sorry if i sound dumb I've just started with Java a week ago. – netbeansnewbie Aug 30 '21 at 14:03

2 Answers2

0

I think you are confusing the Java method with Javascript Number.isInteger(). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger Unfortunately, we don't have this in java directly.

However, you can use the below code to check if double is an integer or not.

        double d = 5;
        if( (d % 1) == 0 ) {
            System.out.println("Yes");
        }
        else{
            System.out.println("No");
        }

PS: This is one of the ways to check if double is an integer or not . If you dig more in java, you will certainly find more ways to do the same.

Sharad
  • 838
  • 1
  • 7
  • 15
0

The Java class Number doesnt have this function. (it exists in javascript though)

Alternatively you can write an easy helper function to solve this problem:

private boolean isInteger(double num)
{
    if (num==(long)num)
        return true;
    
    return false;
}

The code should be pretty self explanatory.