943,929 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2145
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 18th, 2008
0

Re: How to make an array in text file (help..)

Click to Expand / Collapse  Quote originally posted by javaAddict ...
I have tried this:
Java Syntax (Toggle Plain Text)
  1. public static void main(String[] args) {
  2. BufferedReader lnr = null;
  3. String line = null;
  4. try {
  5. lnr = new BufferedReader (new FileReader("accountList.txt"));
  6. for (line = lnr.readLine(); line!=null; line = lnr.readLine()) {
  7.  
  8. String account[] = line.split(" ");
  9.  
  10. System.out.println(account[0]);
  11. }
  12.  
  13. lnr.close();
  14. } catch (Exception e) {
  15. System.out.println("An error has occured: "+e.getMessage());
  16. //e.printStackTrace();
  17. }
  18. }

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...
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Nov 18th, 2008
0

Re: How to make an array in text file (help..)

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:

Java Syntax (Toggle Plain Text)
  1. int N=1000;
  2. String [][] array = new String[N][]
  3. ....
  4. ....
  5. int count=0;
  6. for (....) {
  7.  
  8. array[count] = line.split(" ");
  9. count++;
  10. }

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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 18th, 2008
0

Re: How to make an array in text file (help..)

I have tested and here is an example:

Java Syntax (Toggle Plain Text)
  1. public static void main(String[] args) {
  2. BufferedReader lnr = null;
  3. String line = null;
  4.  
  5. int N=1000;
  6. int totalRecords = 0;
  7.  
  8. String [][] array = new String[N][];
  9.  
  10. try {
  11. lnr = new BufferedReader (new FileReader("accountList.txt"));
  12.  
  13. for (line = lnr.readLine(); line!=null; line = lnr.readLine()) {
  14.  
  15. array[totalRecords] = line.split(" ");
  16. totalRecords++;
  17. }
  18.  
  19. lnr.close();
  20. } catch (Exception e) {
  21. System.out.println("An error has occured: "+e.getMessage());
  22. //e.printStackTrace();
  23. }
  24. for (int i=0;i<totalRecords;i++) {
  25. for (int j=0;j<array[i].length;j++) {
  26. System.out.print(array[i][j]+" ");
  27. }
  28. System.out.println();
  29. }
  30. }

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.
So the 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]
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 21st, 2008
0

Re: How to make an array in text file (help..)

Click to Expand / Collapse  Quote originally posted by javaAddict ...
I have tested and here is an example:

Java Syntax (Toggle Plain Text)
  1. public static void main(String[] args) {
  2. BufferedReader lnr = null;
  3. String line = null;
  4.  
  5. int N=1000;
  6. int totalRecords = 0;
  7.  
  8. String [][] array = new String[N][];
  9.  
  10. try {
  11. lnr = new BufferedReader (new FileReader("accountList.txt"));
  12.  
  13. for (line = lnr.readLine(); line!=null; line = lnr.readLine()) {
  14.  
  15. array[totalRecords] = line.split(" ");
  16. totalRecords++;
  17. }
  18.  
  19. lnr.close();
  20. } catch (Exception e) {
  21. System.out.println("An error has occured: "+e.getMessage());
  22. //e.printStackTrace();
  23. }
  24. for (int i=0;i<totalRecords;i++) {
  25. for (int j=0;j<array[i].length;j++) {
  26. System.out.print(array[i][j]+" ");
  27. }
  28. System.out.println();
  29. }
  30. }

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.
So the 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]

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.
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Quick Java Problem
Next Thread in Java Forum Timeline: Java Video Training





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC