Results 1 to 6 of 6
- 02-06-2012, 02:43 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 7
- Rep Power
- 0
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
- 02-06-2012, 03:49 PM #2
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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
- 02-06-2012, 04:54 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 7
- Rep Power
- 0
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]);
- 02-06-2012, 06:27 PM #4
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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.
- 02-06-2012, 06:33 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 7
- Rep Power
- 0
Re: Reference an array so it can be accessed
Okay, and how would I go about doing that?
- 02-06-2012, 09:12 PM #6
Similar Threads
-
no array reference
By droidus in forum New To JavaReplies: 12Last Post: 10-05-2011, 05:05 AM -
Copying array values without creating just a reference.
By xael in forum New To JavaReplies: 1Last Post: 04-11-2011, 08:25 PM -
Sending array index by reference
By mattie in forum New To JavaReplies: 15Last Post: 02-11-2011, 01:17 PM -
ISSUE: Array as class constructor will not copy elements, only reference
By Lucificate in forum New To JavaReplies: 16Last Post: 07-08-2010, 09:13 PM -
reference to elements in array
By Igor in forum New To JavaReplies: 1Last Post: 12-14-2007, 11:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks