1

I'm in my first year of Java and am having trouble with with a boolean method to test if even or odd. I have most of the code, but when I enter an odd number it returns it as even. My professor gave us the signature of the method: public static boolean isOdd(int number)

package homeWork1;
import java.util.Scanner;

public class OddTest {

   public static void main(String args[])
   {
      int number;
      System.out.println("Enter an integer to check if it is odd or even: ");
      Scanner input = new Scanner(System.in);
      number = input.nextInt();
      isOdd(number);

      boolean answer=isOdd(number);
      if (answer=true)
      {
          System.out.println("EVEN");
      }

      if (answer=false)
      {
          System.out.println("ODD");
      }



   }
   public static boolean isOdd(int number)
   { 
    if(number % 2 == 0)
   {
        return true;
    }
    return false;

}
code_dredd
  • 5,915
  • 1
  • 25
  • 53
James Seeley
  • 11
  • 1
  • 1
  • 2
  • 1
    If it is `%2 == 0` that means it is even... So instead `return false` if that statement is true. else `return true` something like this... `if( (number%2) == 0) { return true;} else {return false;}` – 3kings Sep 10 '16 at 00:33
  • @James, please mark one of the answers to your question as accepted. – Jezor Nov 01 '16 at 01:12

2 Answers2

4

if (number % 2 == 0) means that the number is divisible by 2, and therefore it is even.

You want to change your condition to:

if (number % 2 != 0)

And, as a conditional statement is a boolean value, you can simplify it further like:

public static boolean isOdd(int number)
    return number % 2 != 0;
}

Also, you have an error in the code that uses your method.

  boolean answer=isOdd(number);
  if (answer=true)
  {
      System.out.println("ODD");
  }

  if (answer=false)
  {
      System.out.println("EVEN");
  }

Here, answer=true is an assignment, not comparison. You can fix it by writing:

if (isOdd(number))
{
    System.out.println("ODD");
}

else
{
    System.out.println("EVEN");
}
Jezor
  • 3,253
  • 2
  • 19
  • 43
  • better to use (number&1)==1 . Try isOdd(0x80000000) – ddyer Sep 10 '16 at 00:59
  • 3
    @ddyer I should like to challlenge that. `number % 2 != 0` is much more readable, which should always be your first choice. Besides, I see no other advantage in `(number & 1) == 1`. – Ole V.V. Sep 10 '16 at 01:24
  • number&1 is much faster. 1 cycle on most machines vs many cycles for division with remainder. Handwave about pipelining and smart compilers may help you though. The other thing is that the division may throw a number exception if the dividend is 0x80000000. – ddyer Sep 10 '16 at 09:27
  • @ddyer it will work [only with powers of 2](http://stackoverflow.com/a/3072710/5922757). As the OP seems to be a beginner programmer, it might make him only more confused about what's going on. Also, JVM is smart, I think it can optimize it out by itself. – Jezor Sep 11 '16 at 15:30
0

Instead of checking whether answer is true or false, you're assigning these values. Checking for equality is == in Java. By the way, I'd recommend using just if (answer) and if (!answer) for booleans - looks simpler and less room for a mistake :)

Roman Kotenko
  • 489
  • 7
  • 16
  • Thanks for the help, everything works properly now. Tired eyes didn't catch the == – James Seeley Sep 10 '16 at 00:38
  • 2
    @JamesSeeley you might also want to rename *isOdd* method to *isEven* (or invert the return values and corresponding checks) as it's doing the opposite – Roman Kotenko Sep 10 '16 at 00:42