reading .csv file and getting the data out of it

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 2
Reputation: gadisandeep is an unknown quantity at this point 
Solved Threads: 0
gadisandeep gadisandeep is offline Offline
Newbie Poster

reading .csv file and getting the data out of it

 
0
  #1
Nov 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: reading .csv file and getting the data out of it

 
0
  #2
Nov 20th, 2008
read them all in a list of objects, an object which has two variables: name and number
and then search through that list to check the name that correspond to the number
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: Tyrone.Wilson is an unknown quantity at this point 
Solved Threads: 1
Tyrone.Wilson Tyrone.Wilson is offline Offline
Newbie Poster

Re: reading .csv file and getting the data out of it

 
0
  #3
Nov 20th, 2008
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)

  1. public ArrayList readAsStrings(File filename) throws FileNotFoundException, IOException {
  2. /**returns all of the data in a file as Strings given the File object*/
  3. ArrayList data = new ArrayList();
  4. BufferedReader reader = new BufferedReader(new FileReader(filename));
  5. String nextLine = reader.readLine();
  6. if (filename.exists() && filename.canRead()) {
  7. while (nextLine != null) {
  8. data.add(nextLine);
  9. nextLine = reader.readLine();
  10. }
  11. reader.close();//just a good idea aparently
  12. }
  13. return data;
  14. }
  15.  
  16.  
  17. public ArrayList extractFromCommas(String dataLine) {
  18. //Gives back the data that is found between commas in a String
  19. ArrayList data = new ArrayList();
  20. String theString = "";
  21. for (int i = 0; i < dataLine.length(); i++) {//go down the whole string
  22. if (dataLine.charAt(i) == ',') {
  23. if (i == 0) {
  24. //do nothing
  25. } else {
  26. data.add(theString);//this means that the next comma has been reached
  27. theString = "";//reset theString Variable
  28. }
  29. } else {
  30. theString = theString + dataLine.charAt(i);//otherwise, just keep piling the chars onto the cumulative string
  31. }
  32. }
  33. if (!theString.equalsIgnoreCase(""))//only if the last position is not occupied with nothing then add the end on
  34. {
  35. data.add(theString);
  36. }
  37. return data;
  38. }
  39.  
  40. public ArrayList findString(ArrayList data, String searchString) {
  41. //Finds a string in an arraylist of strings
  42. ArrayList foundStrings = new ArrayList();
  43.  
  44. for (int i = 0; i < data.size(); i++) {
  45. if (data.get(i).toString().contains(searchString)) {
  46. foundStrings.add(data.get(i).toString());
  47. }
  48. }
  49. return foundStrings;//returns null if the string is not found.
  50. }

I would then use this in my main code:

  1.  
  2. public static void main(String[] args) throws FileNotFoundException, DocumentException, BadElementException, MalformedURLException, IOException {
  3. // TODO code application logic here
  4. DataManager dataMan = new DataManager();
  5. ArrayList data = new ArrayList();
  6. ArrayList col1 = new ArrayList();
  7. ArrayList col2 = new ArrayList();
  8.  
  9. data = dataMan.readAsStrings(new File("Directory\\this is my file.csv"));
  10. for(int i=0;i<data.size();i++){
  11. ArrayList temp = new ArrayList();
  12. temp = dataMan.extractFromCommas(data.get(i).toString());
  13. col1.add(temp.get(0).toString());
  14. col2.add(temp.get(1).toString());
  15. }
  16. String name = "What I Am Looking For";
  17. data = dataMan.findString(col1, name);
  18. for(int i=0;i<data.size();i++){
  19. int index = Integer.parseInt(data.get(i).toString());
  20. System.out.println(col1.get(index).toString()+" "+col2.get(index).toString());
  21. }
  22.  
  23. }

I think this will work but will depend on what you want.

hope it helps in any case.

good luck.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC