Results 1 to 6 of 6
  1. #1
    DGates is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Post Reference an array so it can be accessed

    Hi,
    I have set up several arrays so that I can import text from a file which is tab delimited. I have this working fine in the main() method, meaning I can call any of the array elements and print them out to the console.
    However, I want to be able to work on this data, i.e. search all unique things which have a given attribute, which I would do based on user selection. Therefore I have to do this in another method. I am fine for the logic of doing this. But for the life of me don't know how to reference the array I have setup.

    To elaborate, when I want to print out to the console in the main method I can simply do the following:
    System.out.println(x[16]); and this will print out the 16th element in the array. But when I try and do this in another method. The IDE gives me a 'cannot find symbol' error.

    Can anyone suggest how i can resolve this.?
    I have read that perhaps I have to store a reference to the array in a variable, but i'm not sure what this means.

    Any help will be appreciated,
    Thanks,
    Dan

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Reference an array so it can be accessed

    First x[16] doesn't access the 16th element of the array. Arrays are zero based so x[16] is the 17th element in the array. You should post your code, otherwise every response will just be conjecture. My best guess is your array is local only to your main method. You need to declare the array in a way to give your method access to it or you need to pass it as a parameter to this "other" method.

    hope this helps

  3. #3
    DGates is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Reference an array so it can be accessed

    I have included my code below:
    As you can see in the main method, I have declared my arrays, and filled them with strings from the text file.
    The problem is that if I want to access them in other methods, for instance if i wanted to populate a jList. Or if I wanted to calculate something based on an ActionEvent, which would be a separate method. I don't know how to access them.


    Java Code:
    public static void main(String args[]) {
            
        
            String [] a = new String [100];
            String [] b = new String [100];
            String [] c = new String [100];
            String [] d = new String[100];
            String [] e = new String[100];
            String[] f = new String[100];
            String [] g = new String [100];
            String[] h = new String[100];
            String[] i = new String[100];
            String[] j = new String[100];
            String[] k = new String[100];
            String[] l = new String[100];
            String[] m = new String [100]; 
    
            String [] input = new String [17];
            File file = new File("Data.txt");
    
            int line = 0;
            
            
        try {     
               Scanner in = new Scanner(file);
               
               in.useDelimiter("\\t|\\n");
            int i;
            
             for (line =0; line<17; line++){
                
                 for (i=0; i < 13; i++) {
                        input[i] = in.next();
                        //System.out.println(input[i]);
                 }
    
                        a [line] = input [0];
                        b [line] = input [1];
                        c [line] = input [2]; 
                        d [line] = input [3];
                        e [line] = input [4];
                        f [line] = input [5];
                        g [line] = input [6];
                        h [line] = input [7];
                        i [line] = input [8];
                        j [line] = input [9];
                        k [line] = input [10];
                        l [line] = input [11];
                        m [line] = input [12];
                    
                
            }
        }
           catch (FileNotFoundException e) {
               System.out.println("No File");
                e.printStackTrace();
           }
           catch (NumberFormatException nfe){
               System.out.println("A number is of wrong type");
               nfe.getCause();
    }
    
                    System.out.println(g[5]);

  4. #4
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Reference an array so it can be accessed

    I would say given the set-up of your code. I would pass the array you want to process in the method as a parameter of that method.

  5. #5
    DGates is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Reference an array so it can be accessed

    Okay, and how would I go about doing that?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Reference an array so it can be accessed

    The same way as you pass a parameter to any method. You include the parameter type in the method signature, and include the parameter value in the method invocation.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. no array reference
    By droidus in forum New To Java
    Replies: 12
    Last Post: 10-05-2011, 05:05 AM
  2. Replies: 1
    Last Post: 04-11-2011, 08:25 PM
  3. Sending array index by reference
    By mattie in forum New To Java
    Replies: 15
    Last Post: 02-11-2011, 01:17 PM
  4. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  5. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 11:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •