Bookmark Topic Watch Topic
  • New Topic

How do I compare a String to each element of a string array?  RSS feed

 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Report post to moderator
How do I compare a String to each element of a string array?

For example:



This doesn't give me the right value because the IDE says that equals() is an incompatible type. I also tried changing the "heads" to an variable, but the results remains the same.

Any idea? I would prefer using an Array!

Thanks!
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
What type is coins?
you should type this in your question!
I could check that probably it is Coin[], right?

You should be able to test equality of Coin vs String but this is not a good idea (it should always return false).
You might want to convert your Coin to String before comparison.

And the comparison would be

I am assuming your Coin class has overriden toString().

If the array were String[] as you said in the title, your code would work fine (assuming it's inside a loop).
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
You have misunderstood what you have been told about a coin class.

I think you should continue the discussion in your previous thread rather than creating duplicate discussions; I am therefore locking this thread.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!