| | |
reading .csv file and getting the data out of it
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
Hi,I am reading this .CSV file and new to this.I have information in tabular form where i have names in 1row and 1column and in between cells i have numbers indicating links of those names by that purticular number. I want to retrevie those names who are having that purticular number from the correspoinding row and column.
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 1
I have a DataManager Class. this is how I do it (maybe there is a better way I don't know)
In my DataManager Class I have these methods (among others)
I would then use this in my main code:
I think this will work but will depend on what you want.
hope it helps in any case.
good luck.
In my DataManager Class I have these methods (among others)
java Syntax (Toggle Plain Text)
public ArrayList readAsStrings(File filename) throws FileNotFoundException, IOException { /**returns all of the data in a file as Strings given the File object*/ ArrayList data = new ArrayList(); BufferedReader reader = new BufferedReader(new FileReader(filename)); String nextLine = reader.readLine(); if (filename.exists() && filename.canRead()) { while (nextLine != null) { data.add(nextLine); nextLine = reader.readLine(); } reader.close();//just a good idea aparently } return data; } public ArrayList extractFromCommas(String dataLine) { //Gives back the data that is found between commas in a String ArrayList data = new ArrayList(); String theString = ""; for (int i = 0; i < dataLine.length(); i++) {//go down the whole string if (dataLine.charAt(i) == ',') { if (i == 0) { //do nothing } else { data.add(theString);//this means that the next comma has been reached theString = "";//reset theString Variable } } else { theString = theString + dataLine.charAt(i);//otherwise, just keep piling the chars onto the cumulative string } } if (!theString.equalsIgnoreCase(""))//only if the last position is not occupied with nothing then add the end on { data.add(theString); } return data; } public ArrayList findString(ArrayList data, String searchString) { //Finds a string in an arraylist of strings ArrayList foundStrings = new ArrayList(); for (int i = 0; i < data.size(); i++) { if (data.get(i).toString().contains(searchString)) { foundStrings.add(data.get(i).toString()); } } return foundStrings;//returns null if the string is not found. }
I would then use this in my main code:
java Syntax (Toggle Plain Text)
public static void main(String[] args) throws FileNotFoundException, DocumentException, BadElementException, MalformedURLException, IOException { // TODO code application logic here DataManager dataMan = new DataManager(); ArrayList data = new ArrayList(); ArrayList col1 = new ArrayList(); ArrayList col2 = new ArrayList(); data = dataMan.readAsStrings(new File("Directory\\this is my file.csv")); for(int i=0;i<data.size();i++){ ArrayList temp = new ArrayList(); temp = dataMan.extractFromCommas(data.get(i).toString()); col1.add(temp.get(0).toString()); col2.add(temp.get(1).toString()); } String name = "What I Am Looking For"; data = dataMan.findString(col1, name); for(int i=0;i<data.size();i++){ int index = Integer.parseInt(data.get(i).toString()); System.out.println(col1.get(index).toString()+" "+col2.get(index).toString()); } }
I think this will work but will depend on what you want.
hope it helps in any case.
good luck.
![]() |
Similar Threads
- How to read csv file contents in VB.Net (VB.NET)
- Reading in a *.csv file and loading the data into an Array (Java)
- Table Data! (VB.NET)
- How to read data from csv file in an array and parse (C++)
- CSV file (C)
- Reading CSV file into a ADO recordset (ASP.NET)
- Need Help Reading a csv file created from MSExcel (C)
Other Threads in the Java Forum
- Previous Thread: any suggestion : mini project title(simple coding)
- Next Thread: SCJP 5.0 and SCJP 6.0 Mock test - 10 sets
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class classes client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working





