• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

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.
0
DanBAtkinson
Asked:
DanBAtkinson
  • 4
  • 2
  • 2
2 Solutions
 
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
0
 
DanBAtkinsonAuthor Commented:
Thanks for the answer but... Put what after the very first brace?
0
 
objectsCommented:
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];
     }

...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DanBAtkinsonAuthor Commented:
Aha!

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

Thanks again!

Points awarded!
0
 
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.
0
 
CEHJCommented:
:-)

OK - never mind
0
 
objectsCommented:
By making train_station a class (static) variable (instead of a local variable) it is accessible by other static methods in that class.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now