• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with arrays and comparing integers in arrays with a number.

 
Thomas Mullane
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew is much faster!
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:Matthew is much faster!

I'm not sure I'd count 7 seconds as "much"!
 
Thomas Mullane
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million for the help works great now!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Mullane wrote:Thanks a million for the help works great now!

You are welcome
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the 7 seconds bit that is impressive. Anybody can be hours ahead, but 7 seconds apart is good
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic