|
-
December 13th, 2011, 05:57 PM
#1
Help with .csv file to ArrayList
Hi,
Basically I am working on a uni project but I am pretty new to Java. I've been trying to figure out how to do this for days now but just can't get it right. Really need to get it done as the deadline is approaching!
I have 3 (large) tab delimited CSV files that I need the user to be able to search for data inside on a command line input. I'm planning to have a class for putting the CSV files into ArrayLists or another type and a class for handling user input for a start. Is this a good way to go about it?
The part I'm really struggling with is reading a CSV file into an ArrayList. Just can't seem to get my head around it or get it to work despite spending loads of time reading up on it.
I've been fiddling around with some code to try to get an idea of how to put the data into an ArrayList using the tokenizer. When trying to print the arraylist, it seems to show each item twice. I've used two classes, a data manager class and the main one to call it.
The code is like this for the data handling class.
Code:
public class DataManager {
String fileName;
ArrayList <String>storeValues = new ArrayList<String>();
public DataManager(String FileName)
{
this.fileName=FileName;
}
public void ReadFile()
{
try {
BufferedReader br = new BufferedReader( new FileReader(fileName));
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
while( (fileName = br.readLine()) != null)
{
lineNumber++;
System.out.println(fileName);
storeValues.add(fileName);
//break comma separated line using ","
st = new StringTokenizer(fileName, "\t");
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
//reset token number
tokenNumber = 0;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//mutators and accesors
public void setFileName(String newFileName)
{
this.fileName=newFileName;
}
public String getFileName()
{
return fileName;
}
public ArrayList getFileValues()
{
return this.storeValues;
}
public void displayArrayList()
{
for(int x=0;x<this.storeValues.size();x++)
{
System.out.println(storeValues.get(x));
}
}
Main class:
Code:
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName="path to my csv file";
DataManager x=new DataManager(fileName);
x.ReadFile();
x.displayArrayList();
}
}
Any help on this would be great. Also, how would I go about searching for an item using data from more than one ArrayList (from the csv files)? I will be using a scanner to get user input for the word to search and then show data from the class.
Thanks.
-
December 14th, 2011, 06:27 AM
#2
Re: Help with .csv file to ArrayList
You have a CSV file, you comment specifically that the entries are separated by a comma... and still you tokenize it with a tabulator? Is this as intended?
Have you looked at Scanner? While your approach as such is feasable, it is simpler and more readable when coded with Scanner.
Also is the ArrayList right choice here, if so why? I dont know what you actually have in your data. If there is some sort of ID, then maybe HashMap<String, List<String>> would be preferable? If not, then I would suppose storing the tokens is better than the whole String, so you could use List<String>.
Is there sense, in the DataManager not having understanding of multiple CSV files? In which you would have a HashMap<filename, List or Hash as above> of results.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|