We help IT Professionals succeed at work.

Using an array stored in another method

DanBAtkinson
DanBAtkinson asked
on
Medium Priority
245 Views
Last Modified: 2010-07-27
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.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks for the answer but... Put what after the very first brace?
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
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];
     }

...

Author

Commented:
Aha!

I get you!

Author

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

Thanks again!

Points awarded!

Author

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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

OK - never mind
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
By making train_station a class (static) variable (instead of a local variable) it is accessible by other static methods in that class.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.