2

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);
        }
    }


}
3
  • 1
    you overwrite previous values in your while loop, so only the last iteration will stay permanent. Commented Nov 22, 2012 at 16:25
  • Ok thanks for the help jlordo would this be corrected by searching through the lineRead String with a for loop Commented Nov 22, 2012 at 16:27
  • If you want to store every users data, String[] is not a good choice as the data structure. Commented Nov 22, 2012 at 16:28

5 Answers 5

5

The offending line is this:

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

You should put it into some collection, e.g. 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);
    }
Sign up to request clarification or add additional context in comments.

5 Comments

I was going to add exactly this to my answer. OP here is your solution.
@ShyJ, you probably have to call readline() inside the loop, else its an infinite loop.
You better use ArrayList instead of LinkedList: faster in all displinines and more handy to iterate.
Yes I have used a ArrayList instead
Thanks for the help I now see my mistake. How would I go about printing and returning that ArrayList?
2

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.

Comments

1

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.

Comments

1

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);
    }
}

2 Comments

This gives an error at the add section saying "no sutible method found for add" sorry about all the questions I'm new to java and programming.
@user1323808 there was a semicolon missing, try with terminating that line with a semicolon ..
0

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

2 Comments

Vectors are more or less depreciated now, certainly at least obsolete.
From Javadoc: If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.