| | |
How to make an array in text file (help..)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 37
Reputation:
Solved Threads: 0
•
•
•
•
I have tried this:
Java Syntax (Toggle Plain Text)
public static void main(String[] args) { BufferedReader lnr = null; String line = null; try { lnr = new BufferedReader (new FileReader("accountList.txt")); for (line = lnr.readLine(); line!=null; line = lnr.readLine()) { String account[] = line.split(" "); System.out.println(account[0]); } lnr.close(); } catch (Exception e) { System.out.println("An error has occured: "+e.getMessage()); //e.printStackTrace(); } }
The file has these:
And it printed:
12345
55555
what if i will only print out the:
12345???
it seems that the 55555 was printed out also because it was stored in [0] array...so i think,,it should be;;
account[1][0] so that it will only print the 12345.....
and i am really confused either,,,on how to do it....i am not that good in text file really...
It was printed because it was the accountId of the second line of the file. You are the one who wrote the code that loops through each line of the file:
for (....) {
}
With the above code you loop each line and get the values from each line:
account[0], account[1], account[2], ....
Why would you have it any other way? What would be the point of getting only the first line and not the rest of the lines of the files?
Now if you want to store the entire file in a single array, then Yes, use a 2-D array:
I don't know if the above will work. You might want to test it.
If it doesn't then declare the 2-D array: [N][5] and in the loop take the line in a different 1-D array and put the values in the 2-D array.
for (....) {
}
With the above code you loop each line and get the values from each line:
account[0], account[1], account[2], ....
Why would you have it any other way? What would be the point of getting only the first line and not the rest of the lines of the files?
Now if you want to store the entire file in a single array, then Yes, use a 2-D array:
Java Syntax (Toggle Plain Text)
int N=1000; String [][] array = new String[N][] .... .... int count=0; for (....) { array[count] = line.split(" "); count++; }
I don't know if the above will work. You might want to test it.
If it doesn't then declare the 2-D array: [N][5] and in the loop take the line in a different 1-D array and put the values in the 2-D array.
Check out my New Bike at my Public Profile at the "About Me" tab
I have tested and here is an example:
2-D arrays in java don't exist. Actually what you do is create a single array and each element has another array:
So the
array[totalRecords][0],
array[totalRecords][1],
array[totalRecords][2]
You initiall set the max row to be N=1000 because you don't know the size of the file. But you use the totalRecords variable to count the lines, because inside the loop you don't an int variable so you must do:
array[totalRecords] = ...
totalRecords++
in order to place the next line to the next row.
When you print the values you don't use the N at the first loop because not all rows of the array have value. You use the totalRecords. Remember you have used to count the lines read in the first loop.
The second inner loop you say:
array[i][0], array[i][1] : array[i][j]
Java Syntax (Toggle Plain Text)
public static void main(String[] args) { BufferedReader lnr = null; String line = null; int N=1000; int totalRecords = 0; String [][] array = new String[N][]; try { lnr = new BufferedReader (new FileReader("accountList.txt")); for (line = lnr.readLine(); line!=null; line = lnr.readLine()) { array[totalRecords] = line.split(" "); totalRecords++; } lnr.close(); } catch (Exception e) { System.out.println("An error has occured: "+e.getMessage()); //e.printStackTrace(); } for (int i=0;i<totalRecords;i++) { for (int j=0;j<array[i].length;j++) { System.out.print(array[i][j]+" "); } System.out.println(); } }
2-D arrays in java don't exist. Actually what you do is create a single array and each element has another array:
String [][] array = new String[N][]; //2-D array with N rows
array[totalRecords] = line.split(" "); // the row: totalRecords will have the array returned by the split method.array[totalRecords] has another array inside it and to access it you do:array[totalRecords][0],
array[totalRecords][1],
array[totalRecords][2]
You initiall set the max row to be N=1000 because you don't know the size of the file. But you use the totalRecords variable to count the lines, because inside the loop you don't an int variable so you must do:
array[totalRecords] = ...
totalRecords++
in order to place the next line to the next row.
When you print the values you don't use the N at the first loop because not all rows of the array have value. You use the totalRecords. Remember you have used to count the lines read in the first loop.
The second inner loop you say:
array[i].length because the array[i] is also a single array and in the inner loop you print its values:array[i][0], array[i][1] : array[i][j]
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Oct 2008
Posts: 37
Reputation:
Solved Threads: 0
•
•
•
•
I have tested and here is an example:
Java Syntax (Toggle Plain Text)
public static void main(String[] args) { BufferedReader lnr = null; String line = null; int N=1000; int totalRecords = 0; String [][] array = new String[N][]; try { lnr = new BufferedReader (new FileReader("accountList.txt")); for (line = lnr.readLine(); line!=null; line = lnr.readLine()) { array[totalRecords] = line.split(" "); totalRecords++; } lnr.close(); } catch (Exception e) { System.out.println("An error has occured: "+e.getMessage()); //e.printStackTrace(); } for (int i=0;i<totalRecords;i++) { for (int j=0;j<array[i].length;j++) { System.out.print(array[i][j]+" "); } System.out.println(); } }
2-D arrays in java don't exist. Actually what you do is create a single array and each element has another array:
So theString [][] array = new String[N][]; //2-D array with N rows array[totalRecords] = line.split(" "); // the row: totalRecords will have the array returned by the split method.array[totalRecords]has another array inside it and to access it you do:
array[totalRecords][0],
array[totalRecords][1],
array[totalRecords][2]
You initiall set the max row to be N=1000 because you don't know the size of the file. But you use the totalRecords variable to count the lines, because inside the loop you don't an int variable so you must do:
array[totalRecords] = ...
totalRecords++
in order to place the next line to the next row.
When you print the values you don't use the N at the first loop because not all rows of the array have value. You use the totalRecords. Remember you have used to count the lines read in the first loop.
The second inner loop you say:array[i].lengthbecause the array[i] is also a single array and in the inner loop you print its values:
array[i][0], array[i][1] : array[i][j]
oh,,i am sorry if i have read your reply too late...thank you very much for your help,,thank you very much,,,,ok,, i will do everything you told me,,it seems that i am too busy...thank you very much for the help....(^.^)
Last edited by l_03; Nov 21st, 2008 at 7:37 am.
![]() |
Similar Threads
- Text file parser in C (C)
- Scanning a text file for a string (C++)
- turning userinput into a text file (C)
- Trying to obtain text from a file. (C#)
- Trying to creating an array from a text file (C++)
- string: get token from string and compare token from text file (C++)
- Parsing Text file (Java)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the Java Forum
- Previous Thread: Quick Java Problem
- Next Thread: Java Video Training
| Thread Tools | Search this Thread |
actionlistener android api applet application array arrays automation binary block bluetooth character chat class classes client code compile component consumer database desktop developmenthelp eclipse error event exception fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number online page print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update windows working






