| Author |
Need help with arrays and comparing integers in arrays with a number.
|
Thomas Mullane
Greenhorn
Joined: Jan 29, 2012
Posts: 3
|
|
I'm trying to write a method in Java that takes an array of ints (myArray) and an int (num) and returns a boolean value to say whether or not number is in myArray but using compareTo. However don't know how to use it too well and and stuck on it. Would love help as I'm a beginner programmer. Here is my code so far!
[Edit - added code tags - see UseCodeTags]
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
Hi Thomas. Welcome to The Ranch!
int is a primitive type - it doesn't have any methods. So you can't call compareTo on it. Just use ==
A couple of other tips:
- Instead of if (condition) output = true; else output = false;, it's much easier to read if you just write output = condition;
- (Edit - deleted comment on brackets - my mistake!)
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Welcome to JavaRanch Thomas Mullane !
As error stated base type does not have methods. use == instead of compareTo as in below
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Matthew is much faster!
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
Seetharaman Venkatasamy wrote:Matthew is much faster! 
I'm not sure I'd count 7 seconds as "much"!
|
 |
Thomas Mullane
Greenhorn
Joined: Jan 29, 2012
Posts: 3
|
|
|
Thanks a million for the help works great now!
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Thomas Mullane wrote:Thanks a million for the help works great now!
You are welcome
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
It is the 7 seconds bit that is impressive. Anybody can be hours ahead, but 7 seconds apart is good
|
 |
 |
|
|
subject: Need help with arrays and comparing integers in arrays with a number.
|
|
|