Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing a String to a String in an Array

 
Brandon Golway
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I converted a game that I wrote in Python to java (since Im currently learning it in my college class) and I cant figure out how to compare a string to a string in an array without doing a lot of coding. All I basically want is if the player finds a weapon it gets added to their weapon array, then when you call the inventory function it will list the players weapons.

Heres the simpliest excerpt I could make


the first line of ListWeapons() throws a NullPointerException
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stupid question- Do you actually ever call AddWeapon (which should be addWeapon, by the way) with each parameter?

Posting an SSCCE would help clear up any more confusion about what you're doing, which would help you get assistance faster.
 
Mike Peters
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may consider using a set:

 
Brandon Golway
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked, thanks Mike!
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Workman wrote:Stupid question
There are no stupid questions only stupid answers. Remember that we'll had to learn Java at one point.
 
Mike Peters
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure Brandon. Good luck and don't hesitate to ask questions!
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:
Kevin Workman wrote:Stupid question
There are no stupid questions only stupid answers. Remember that we'll had to learn Java at one point.


I was saying my question was stupid, not the OP's. It's easy to miss the "obvious" stuff when you've been staring at the same code for days, so sometimes all it takes is somebody else asking a stupid question to help you along.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha then I misinterpreted it. But my post still stands.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic