wood burning stoves*
The moose likes Beginning Java and the fly likes Need help with arrays and comparing integers in arrays with a number. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with arrays and comparing integers in arrays with a number." Watch "Need help with arrays and comparing integers in arrays with a number." New topic
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: 4240
    
    7

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: 4240
    
    7

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: 36507
    
  16
It is the 7 seconds bit that is impressive. Anybody can be hours ahead, but 7 seconds apart is good
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with arrays and comparing integers in arrays with a number.
 
Similar Threads
Array Question
postfix and prefix increment precedence in conditional expressions
Question on Palindromes
Constructing multidimensional arrays
reverse array