-5

There are several ways to decide if a value is an integer or not. All of the ways i know are using the divide operation.

Which method is the fastest and: Is there a method to do this without doing floating point operations?

EDIT: For clarification, my program is only dealing with integer and double values. Here is some of my code:

 for (int i = 6;; i++) {

        for (int j = i - 1; j > 0; j--) {
            double number1 = i;
            double number2 = j;
            double d = number1 / number2;
            int help = (int)d;
            if( (d - help) == 0.0){
                System.out.println("Whole number found");
            }
        }
}
Officer Bacon
  • 724
  • 1
  • 7
  • 22
  • 3
    What is your input? Text? –  Feb 12 '15 at 13:42
  • No, only numbers. Mostly int / int – Officer Bacon Feb 12 '15 at 13:43
  • 1
    What is 'int / int'? Show us some Java code so we can understand what you are trying to do. –  Feb 12 '15 at 13:44
  • This question has been asked many times before. Did you read [this article](http://stackoverflow.com/questions/237159/whats-the-best-way-to-check-to-see-if-a-string-represents-an-integer-in-java)? – user1438038 Feb 12 '15 at 13:44
  • 1
    Could you please show one of the ways you know and wish to avoid as an illustration? – Sergey Kalinichenko Feb 12 '15 at 13:44
  • 5 / 3 , 16 / 3 , 28 / 15 ... – Officer Bacon Feb 12 '15 at 13:45
  • 2
    @user1438038 We are still not sure if the input is a String. –  Feb 12 '15 at 13:45
  • @OfficerBacon Please describe in more detail what you are trying to do. As it is now, your question will be closed because it is unclear what you ask. –  Feb 12 '15 at 13:47
  • Is is possible that what you actually want to do is checking whether an integer `a` is divisible by another integer `b`? If so, `b * (a / b) == a` would do the trick without floating point arithmetic or string representations. – 5gon12eder Feb 12 '15 at 13:48
  • If this question was about JavaScript, I would understand. This way it seems completely misguided. *The static type system* will tell you if a "value" is an integer. The program will *refuse to compile* if you try to pass a non-integer to a method expecting an integer. And if that doesn't give you the answer you expect, then fix your question to something with more substance. – Marko Topolnik Feb 12 '15 at 13:48

2 Answers2

4

Note that int / int will also be an int due to the rules of integral division. You need to promote one of them to floating point or use the idiom 1.0 * a / b if you want to retain a remainder.

If you have a floating point number, f, then

java.lang.Math.floor(f) - f == 0 is probably the best way.

It avoids an intermediate cast to int which can overflow.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

This will do what you want!

    String str = "1";
    try {
        Integer.parseInt(str);
    } catch (NumberFormatException e) {
        System.err.println("Not a number");
    }
kism3t
  • 1,343
  • 1
  • 14
  • 33