• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Searching Array

 
Ranch Hand
Posts: 42
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:
Yes. Put it between lines 1 and 2.



Thanks for your help.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic