Using an array stored in another method

Hi there.

I have a small problem which has suddenly baffled me.

I have a class which has an array of data, and method which utilises it in searching for that data to determine the length and position of a value. The class which contains the data is called Stations.java and Application.java contains the utilisation code.

My problem is this... I have used a search method to find a value in the station array but I can't seem to access the array using the method.

What am I doing wrong? I feel a bit stupid here!




=========Stations.java=========

public class Stations
{
      public static String StationList(int index)
      {
            String station;
            
            String[] train_station = {
                  "Walthamstow Central",
                  "Blackhorse Road",
                  "Tottenham Hale",
                  "Seven Sisters",
                  "Finsbury Park",
                  "Highbury and Islington",
                  "King's Cross and St Pancras",
                  "Euston",
                  "Warren Street",
                  "Oxford Circus",
                  "Green Park",
                  "Victoria",
                  "Pimlico",
                  "Vauxhall",
                  "Stockwell",
                  "Brixton",
                  };
            station = train_station[index];

            return station;
      }
      
      public static int Search(String searchV)
      {
                  for (int i=0; i < train_station.length; i++)
        {
           // you need to use equals() method to check if two strings have same value
           // The == operator just checks if they are same object, not that have the same value
     
           if (train_station[i].equals(searchV))  
              return i;     // return the position
        }
        return -1; // This indicates not found
   }
}


Any help would be grateful.

Thanks.
DanBAtkinsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
final static String[] train_station = {
               "Walthamstow Central",
               "Blackhorse Road",
               "Tottenham Hale",
               "Seven Sisters",
               "Finsbury Park",
               "Highbury and Islington",
               "King's Cross and St Pancras",
               "Euston",
               "Warren Street",
               "Oxford Circus",
               "Green Park",
               "Victoria",
               "Pimlico",
               "Vauxhall",
               "Stockwell",
               "Brixton",
               };


and put it after the very first brace
DanBAtkinsonAuthor Commented:
Thanks for the answer but... Put what after the very first brace?
Mick BarryJava DeveloperCommented:
public class Stations
{
         static String[] train_station = {
               "Walthamstow Central",
               "Blackhorse Road",
               "Tottenham Hale",
               "Seven Sisters",
               "Finsbury Park",
               "Highbury and Islington",
               "King's Cross and St Pancras",
               "Euston",
               "Warren Street",
               "Oxford Circus",
               "Green Park",
               "Victoria",
               "Pimlico",
               "Vauxhall",
               "Stockwell",
               "Brixton",
               };
 
    public static String StationList(int index)
     {
           return train_station[index];
     }

...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

DanBAtkinsonAuthor Commented:
Aha!

I get you!
DanBAtkinsonAuthor Commented:
That's what I was trying to do, but it wouldn't work for one reason or another!

Thanks again!

Points awarded!
DanBAtkinsonAuthor Commented:
Sorry. I meant points awarded to CEHJ. I got what he meant and then saw objects response!

400 points awarded to CEHJ and 100 points awarded to objects.
CEHJCommented:
:-)

OK - never mind
Mick BarryJava DeveloperCommented:
By making train_station a class (static) variable (instead of a local variable) it is accessible by other static methods in that class.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.