简体   繁体   中英

Java - Adding sections of a txt file to an array

I have imported a .csv database file that lists the users of the program, along with other information in the form : UserName, Password, PropertyName, EstimatedValue.

I have figured how to get the username but it will only read the last username on the database and not the others. Help would be greatly appreciated.

import java.util.*;
import java.io.*;

public class readCSV
{
    String[] userData;

    public void checkLogin() throws IOException
    {
        try
        {
            File file = new File("C:/Users/Sean/Documents/Programming assigment/Users.csv");
            BufferedReader bufRdr  = new BufferedReader(new FileReader(file));      
            String lineRead = bufRdr.readLine();
            while(lineRead != null)
            {
                this.userData = lineRead.split(",");
                lineRead = bufRdr.readLine();
            }
            bufRdr.close();
        }
        catch(Exception er){
            System.out.print(er); 
            System.exit(0);
        }
    }


}

The offending line is this:

this.userData = lineRead.split(",");

You should put it into some collection, eg a list

final List<String[]> userData = new LinkedList<String[]> ();

try
    {
        File file = new File("C:/Users/Sean/Documents/Programming assigment/Users.csv");
        BufferedReader bufRdr  = new BufferedReader(new FileReader(file));      
        String lineRead = bufRdr.readLine();
        while(lineRead != null)
        {
            this.userData.add (lineRead.split(","));
        }
        bufRdr.close();
    }
    catch(Exception er){
        System.out.print(er); 
        System.exit(0);
    }

Your line;

this.userData = lineRead.split(",");

overwrites the value of this.userData with each iteration, the result is that it just holds the value from the final iteration.

your String[] (userData) is being replaced/overwritten on every iteration, you will have to Store them in an array/collection.

List<String[]> list = new ArrayList<String[]>();
while((lineRead=bufRdr.readLine())!= null)
        {
            this.userData = lineRead.split(",");
            list.add(this.userData);
        }
        bufRdr.close();

To print the contents:

for(String[] str : list){
    for(String s: str){
       System.out.pritnln(s);
    }
}

If you want to read many useres you need an ArrayList of userdata:
Where this.userData is defined as

 ArrayList<UserData> userDataList;

and in your loop:

 while(lineRead != null)
 {
      this.userDataList.add(lineRead.split(","));
      lineRead = bufRdr.readLine();
 }

Your current code loops through all names, but overwrites the value in each iteration. Finally only the last value is kept.

The problem is that in your while loop you are assigning your string to the same variable... so once you have read the entire file.. the variable holds the last value only.

What you need to do is:

Vector<String> userData = new Vector<String>();

then in your loop...

userData.add(lineRead);

then later you can split each one and do additional processing at that time....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM