Results 1 to 8 of 8
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default How to test for an odd/even number

    Hi I'm very new to java just trying to answer some learning questions from college. (not allowed to use jpb in the question)

    It asks that a very basic program takes in from args[] two command line arguements, then tests them to see if their odd or even. I'm just wondering is there any pre made methods that I could use that tests if an integer is odd or even. I haven't wrote any of the program yet just wondering about any pre made methods.

    If not I was thinking of taking the string arguements from args[] and using Integer.parseint() to turn them into integers. Then divide them by an odd or even number and see if the answer has a decimal point or not to show if its odd or even (might completely fail, thats all i thought off at the moment)

    Thanks for all help in advance

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    As far as I know there is no pre-made method for determing whether or not a number is even or odd, however it is very simple and you are on the right track.

    I would suggest you read up on the "%" operator.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    There is no specific pre-defined way to do that. Use the simple logic, with the array length/size. Check the mod value for validation.

  4. #4
    orion_mcl is offline Member
    Join Date
    Aug 2007
    Posts
    17
    Rep Power
    0

    Default

    Hi. I'm sure there's not a pre made method to check if a number is odd or even. And if you don't want to use the "if" statement you can also user the "?" operator. Check this out :)

    value=Integer.parseInt(.......//HERE GOES THE INPUT
    boolean isEven=(value%2==0)?true:false;

    I hope you can find this useful. Bye.

  5. #5
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    I read up on % and decided to test for even in a if statement. Thanks all for the help!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by orion_mcl View Post
    Hi. I'm sure there's not a pre made method to check if a number is odd or even. And if you don't want to use the "if" statement you can also user the "?" operator. Check this out :)

    value=Integer.parseInt(.......//HERE GOES THE INPUT
    boolean isEven=(value%2==0)?true:false;

    I hope you can find this useful. Bye.
    Isn't using the ternary operator a bit overkill? What does it add here but unnecessary complexity?

    See for yourself, which is cleaner?

    A:
    Java Code:
    boolean isEven = (value % 2 == 0);


    vs. B (your example):
    Java Code:
    boolean isEven = (value % 2 == 0) ? true : false;

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I agreed with Fubarable. Unnecessary complexities make trouble all the time. Small code segments like this is okay, but with the huge applications, it's very difficult to track those things. And also there is no huge difference in performance wise in two ways above.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by counterfox View Post
    I read up on % and decided to test for even in a if statement. Thanks all for the help!
    You are welcome. :)

    If you've satisfied with the answer, please mark the thread solved from the Thread Tools menu.

Similar Threads

  1. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 12:37 AM
  2. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM
  3. Replies: 1
    Last Post: 08-07-2009, 12:14 PM
  4. Replies: 1
    Last Post: 11-03-2008, 02:38 PM
  5. Replies: 0
    Last Post: 04-05-2008, 12:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •