Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching Array  RSS feed

 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an array of names. I am attempting to search the array for specific names from what the user types in. My search code works when the name being search is not from user input, but once I set the parameters in my linearSearch method to take user input it will not find the String being search for.

 
Carey Brown
Bartender
Posts: 3027
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are searching 'args', not 'names'.

You should be declaring names as

This is assuming that NAMES will not be changing. This is how to declare constants, and constant names should be in all upper case.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You are searching 'args', not 'names'.

You should be declaring names as

This is assuming that NAMES will not be changing. This is how to declare constants, and constant names should be in all upper case.


I tried what you said, but changing my array to, private static final String[] NAMES = {..names here..} gave me an error (illegal start of expression). I also tried to put just the name of the array, names, in all caps, NAMES, but it still would not take the user input.
 
Carey Brown
Bartender
Posts: 3027
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott M Summers wrote:
Carey Brown wrote:You are searching 'args', not 'names'.

You should be declaring names as

This is assuming that NAMES will not be changing. This is how to declare constants, and constant names should be in all upper case.


I tried what you said, but changing my array to, private static final String[] NAMES = {..names here..} gave me an error (illegal start of expression). I also tried to put just the name of the array, names, in all caps, NAMES, but it still would not take the user input.

Sorry, my suggestion only works if you declare it just inside your class declaration, and not inside a method as you have it.

You should not compare strings using ==, use equals() instead.

The == only compares the references and not the value stored at the references. Two different string containing "joker" will not have the same reference.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Sorry, my suggestion only works if you declare it just inside your class declaration, and not inside a method as you have it.

You should not compare strings using ==, use equals() instead.

The == only compares the references and not the value stored at the references. Two different string containing "joker" will not have the same reference.


Thank you, that worked. When you say "my suggestion only works if you declare it just inside your class declaration" is the class declaration in between lines 1 & 2 of the code below?
 
Carey Brown
Bartender
Posts: 3027
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott M Summers wrote:Thank you, that worked. When you say "my suggestion only works if you declare it just inside your class declaration" is the class declaration in between lines 1 & 2 of the code below?

Yes. Put it between lines 1 and 2.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Yes. Put it between lines 1 and 2.


Thanks for your help.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!