Results 1 to 8 of 8
- 04-14-2010, 07:01 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
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
- 04-14-2010, 07:07 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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.
- 04-14-2010, 07:20 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 04-14-2010, 08:17 PM #4
Member
- Join Date
- Aug 2007
- Posts
- 17
- Rep Power
- 0
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.
- 04-14-2010, 10:43 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
I read up on % and decided to test for even in a if statement. Thanks all for the help!
-
- 04-15-2010, 02:51 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 04-15-2010, 02:52 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Finding a number in array close to another number
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 02-15-2010, 12:37 AM -
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02-05-2010, 10:42 PM -
Geting Mobile Number, Mobile Operator, Location and Mobile Serial Number by J2ME.
By maruffaiz in forum CLDC and MIDPReplies: 1Last Post: 08-07-2009, 12:14 PM -
how to extract the number of the image which looks like a number
By Crest.Boy in forum Java ServletReplies: 1Last Post: 11-03-2008, 02:38 PM -
Test Advisory Panel-Telecommute- Test your Java skills + share insights on Java tests
By michelle in forum Jobs OfferedReplies: 0Last Post: 04-05-2008, 12:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks