943,884 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2145
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 17th, 2008
0

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

Expand Post »
hello guyzz,,,i am almost finished with my code,,but our teacher require us to access a text file..this is it:

in my text file:

accountList.txt

12345 1234 200000 Jerry Lopez



first column was accountNumber, then the pinNumber, then the balance and the name..

what i did was...

import java.io.*;
public class BankAccount{

public void account(){

File ACCOUNT = new File ("accountList.txt");
FileReader fr = new FileReader(FROZEN);
Reader reader1 = new BufferedReader (fr);
LineNumberReader lnr = new LineNumberReader (reader1);
String line;
for (line = lnr.readLine(); line!=null; line = lnr.readLine()){

String account[] = new String[4];
int count = 0;


int accountNumber[] = Integer.parseInt(account[count][1]);
int pinNumber[] = Integer.parseInt(account[count][2];
int balance[] = Integer.parseInt(account[count][3];
String name = account[count][4];


System.out.println(accountNumber[0][1]);
}
}


i want to sysout the accountNumber,,which is
12345

it has no error when i run it,,but i dont really know what is the problem...

it only print out
0

and i dont know why zero,,,,it seems there is a problem when i declare it...

help guyzzz...i really need your help because we are about to pass it next week,,,,

"that is a shortcut,,i already have the main method.."
Similar Threads
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Nov 17th, 2008
0

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

int accountNumber[] = Integer.parseInt(account[count][1]);
int pinNumber[] = Integer.parseInt(account[count][2];
int balance[] = Integer.parseInt(account[count][3];
String name = account[count][4];

i dont know wat exactly r u doing here
u created account[] is a String Single Dimension array and we are accessing it as a double dimension array


-------------------------My View---
now u got the line from the File ri8
suppose say
12345 1234 200000 Jerry Lopez
step1 : split the line with space as delimiter
and write each in a String array ,
say names(array name) names[0]=jerryu Lopez
names[1]=200000
names[2]=1234
names[3]=12345
step2: Now Create an HashMap map =new HashMap();
map.put(names[0],names[1]);
map.put(names[0],names[2]);
map.put(names[0],names[3]);

Read Hash map For this
Now list The entires in hash map using the map name .. "names[0]"
Click to Expand / Collapse  Quote originally posted by l_03 ...
hello guyzz,,,i am almost finished with my code,,but our teacher require us to access a text file..this is it:

in my text file:

accountList.txt

12345 1234 200000 Jerry Lopez



first column was accountNumber, then the pinNumber, then the balance and the name..

what i did was...

import java.io.*;
public class BankAccount{

public void account(){

File ACCOUNT = new File ("accountList.txt");
FileReader fr = new FileReader(FROZEN);
Reader reader1 = new BufferedReader (fr);
LineNumberReader lnr = new LineNumberReader (reader1);
String line;
for (line = lnr.readLine(); line!=null; line = lnr.readLine()){

String account[] = new String[4];
int count = 0;


int accountNumber[] = Integer.parseInt(account[count][1]);
int pinNumber[] = Integer.parseInt(account[count][2];
int balance[] = Integer.parseInt(account[count][3];
String name = account[count][4];


System.out.println(accountNumber[0][1]);
}
}


i want to sysout the accountNumber,,which is
12345

it has no error when i run it,,but i dont really know what is the problem...

it only print out
0

and i dont know why zero,,,,it seems there is a problem when i declare it...

help guyzzz...i really need your help because we are about to pass it next week,,,,

"that is a shortcut,,i already have the main method.."
Reputation Points: 10
Solved Threads: 1
Light Poster
babusek is offline Offline
28 posts
since Oct 2008
Nov 17th, 2008
0

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

Look at your code:
Java Syntax (Toggle Plain Text)
  1. for (line = lnr.readLine(); line!=null; line = lnr.readLine()){
  2.  
  3. String account[] = new String[4];
  4. int count = 0;
  5.  
  6.  
  7. int accountNumber[] = Integer.parseInt(account[count][1]);
  8. int pinNumber[] = Integer.parseInt(account[count][2];
  9. int balance[] = Integer.parseInt(account[count][3];
  10. String name = account[count][4];
  11.  
  12.  
  13. System.out.println(accountNumber[0][1]);
  14. }

You read the line BUT inside the loop you don't do anything with it. Instead you call this:
Integer.parseInt(account[count][1]); .
The account array is not 2-D and the Integer.parseInt does not return an array.


Now inside the line you have something like this:
12345 1234 200000 Jerry Lopez

So If you use the split method:
Java Syntax (Toggle Plain Text)
  1. String [] array = line.split(" ");

The elements of the array would be:
0: 12345
1: 1234
2: 200000
3: Jerry
4: Lopez

Also for reading from a file try this shorter version:
Java Syntax (Toggle Plain Text)
  1. BufferedReader lnr = new BufferedReader (new FileReader("accountList.txt"));
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 17th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by javaAddict ...
Look at your code:
Java Syntax (Toggle Plain Text)
  1. for (line = lnr.readLine(); line!=null; line = lnr.readLine()){
  2.  
  3. String account[] = new String[4];
  4. int count = 0;
  5.  
  6.  
  7. int accountNumber[] = Integer.parseInt(account[count][1]);
  8. int pinNumber[] = Integer.parseInt(account[count][2];
  9. int balance[] = Integer.parseInt(account[count][3];
  10. String name = account[count][4];
  11.  
  12.  
  13. System.out.println(accountNumber[0][1]);
  14. }

You read the line BUT inside the loop you don't do anything with it. Instead you call this:
Integer.parseInt(account[count][1]); .
The account array is not 2-D and the Integer.parseInt does not return an array.


Now inside the line you have something like this:
12345 1234 200000 Jerry Lopez

So If you use the split method:
Java Syntax (Toggle Plain Text)
  1. String [] array = line.split(" ");

The elements of the array would be:
0: 12345
1: 1234
2: 200000
3: Jerry
4: Lopez

Also for reading from a file try this shorter version:
Java Syntax (Toggle Plain Text)
  1. BufferedReader lnr = new BufferedReader (new FileReader("accountList.txt"));



oh,,thank you very much for the help guyzz,,,this will be a great help...

ah now i see,,,

so if i will use string.split...

how will i declare the variables?????will it be the same with what i did???...i am really confused anyway...
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Nov 17th, 2008
0

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

Java Syntax (Toggle Plain Text)
  1. for (line = lnr.readLine(); line!=null; line = lnr.readLine()){
  2.  
  3. String account[] = line.split(" ");
  4.  
  5. //print whatever you want
  6. //and do whatever you want with the elements:
  7. //account[0], account[1], account[2], account[3], account[4]
  8.  
  9. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 17th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by javaAddict ...
Java Syntax (Toggle Plain Text)
  1. for (line = lnr.readLine(); line!=null; line = lnr.readLine()){
  2.  
  3. String account[] = line.split(" ");
  4.  
  5. //print whatever you want
  6. //and do whatever you want with the elements:
  7. //account[0], account[1], account[2], account[3], account[4]
  8.  
  9. }



thank you,,,thank you very much "java addict"...this is such a great help...thank you....,,
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Nov 17th, 2008
0

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

for (line = lnr.readLine(); line!=null; line = lnr.readLine()){

String account[] = line.split(" ");

//print whatever you want
//and do whatever you want with the elements:
//account[0], account[1], account[2], account[3], account[4]

}

hello, i have tried what you told me...

but when i print
System.out.println(account[0]);

it prints the whole line which is:

12345 1234 200000 Jerry Lopez

i think this is a two dimensional array...

i only want to print out the 12345...but i am really confused either....

thank you in advance........
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Nov 17th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by javaAddict ...
Java Syntax (Toggle Plain Text)
  1. for (line = lnr.readLine(); line!=null; line = lnr.readLine()){
  2.  
  3. String account[] = line.split(" ");
  4.  
  5. //print whatever you want
  6. //and do whatever you want with the elements:
  7. //account[0], account[1], account[2], account[3], account[4]
  8.  
  9. }


for (line = lnr.readLine(); line!=null; line = lnr.readLine()){

String account[] = line.split(" ");

//print whatever you want
//and do whatever you want with the elements:
//account[0], account[1], account[2], account[3], account[4]

}

hello, i have tried what you told me...

but when i print
System.out.println(account[0]);

it prints the whole line which is:

12345 1234 200000 Jerry Lopez

i think this is a two dimensional array...

i only want to print out the 12345...but i am really confused either....

thank you in advance........


what if i have this in my accountList.txt...
12345 1234 200000 Jerry Lopez
23456 2345 45666 Letty Mae

what if i want to print out the 23456?????....

i am really confused either...maybe i am not that good in text file and how to make arrays
with it....

i hope you can help me...
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..)

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:
Quote ...
12345 1234 200000 Jerry Lopez
55555 1234 200000 Jerry Lopez
And it printed:

12345
55555
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..)

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

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