DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   How to make an array in text file (help..) (http://www.daniweb.com/forums/thread157844.html)

l_03 Nov 17th, 2008 7:58 am
How to make an array in text file (help..)
 
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.."

babusek Nov 17th, 2008 8:31 am
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]"
Quote:

Originally Posted by l_03 (Post 737794)
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.."


javaAddict Nov 17th, 2008 8:35 am
Re: How to make an array in text file (help..)
 
Look at your code:
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]);
}

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:
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:
BufferedReader lnr = new BufferedReader (new FileReader("accountList.txt"));

l_03 Nov 17th, 2008 8:48 am
Re: How to make an array in text file (help..)
 
Quote:

Originally Posted by javaAddict (Post 737817)
Look at your code:
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]);
}

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:
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:
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...

javaAddict Nov 17th, 2008 8:53 am
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]

}

l_03 Nov 17th, 2008 9:07 am
Re: How to make an array in text file (help..)
 
Quote:

Originally Posted by javaAddict (Post 737829)
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]

}




thank you,,,thank you very much "java addict"...this is such a great help...thank you....,,

l_03 Nov 17th, 2008 8:23 pm
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........

l_03 Nov 17th, 2008 8:32 pm
Re: How to make an array in text file (help..)
 
Quote:

Originally Posted by javaAddict (Post 737829)
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]

}



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...

javaAddict Nov 18th, 2008 7:03 am
Re: How to make an array in text file (help..)
 
I have tried this:
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:
Quote:

12345 1234 200000 Jerry Lopez
55555 1234 200000 Jerry Lopez
And it printed:

12345
55555

l_03 Nov 18th, 2008 7:14 am
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...

l_03 Nov 18th, 2008 7:14 am
Re: How to make an array in text file (help..)
 
Quote:

Originally Posted by javaAddict (Post 738590)
I have tried this:
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...

javaAddict Nov 18th, 2008 8:28 am
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:

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.

javaAddict Nov 18th, 2008 8:49 am
Re: How to make an array in text file (help..)
 
I have tested and here is an example:

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

l_03 Nov 21st, 2008 7:36 am
Re: How to make an array in text file (help..)
 
Quote:

Originally Posted by javaAddict (Post 738636)
I have tested and here is an example:

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.
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....(^.^)


All times are GMT -4. The time now is 10:06 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC