Results 1 to 11 of 11

Thread: parsing CSV

  1. #1
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default parsing CSV

    Hello, I am making my own CSV parser, but I am coming across problems.
    Here is the text file I am trying to parse:
    Java Code:
    col1, col2, col3, col4
    1,2,3,4
    11,12,13,14
    21,22,23,24
    31,32,33,34
    Here is the result I get in my program from parsing it:
    Java Code:
    col1, col2, col3, col4
    1,2,3,4
    11,12,13,14
    So for some reason, it is not parsing all of it :S

    Help would be much appreciated
    Thanks allot.
    Here is my code:
    Java Code:
    		ArrayList<String> headers = new ArrayList<String>();
    		ArrayList<String> oneDdata = new ArrayList<String>();
    		Scanner lineScan = new Scanner(f);
    		Scanner s = new Scanner(lineScan.nextLine());
    		s.useDelimiter(",");
    		while (s.hasNext()) {
    			headers.add(s.next());
    		}
    
    		while (lineScan.hasNextLine()) {
    			s = new Scanner(lineScan.nextLine());
    			s.useDelimiter(",");
    			while (s.hasNext()) {
    				oneDdata.add(s.next());
    			}
    		}

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    where is the code that prints the result?

  3. #3
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Here is the full code, sorry, I should have posted this first.
    Your help is much appreciated:
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    
    public class CSVparser {
    	public static void parse(File f) throws FileNotFoundException {
    		ArrayList<String> headers = new ArrayList<String>();
    		ArrayList<String> oneDdata = new ArrayList<String>();
    		Scanner lineScan = new Scanner(f);
    		Scanner s = new Scanner(lineScan.nextLine());
    		s.useDelimiter(",");
    		while (s.hasNext()) {
    			headers.add(s.next());
    		}
    
    		while (lineScan.hasNextLine()) {
    			s = new Scanner(lineScan.nextLine());
    			s.useDelimiter(",");
    			while (s.hasNext()) {
    				oneDdata.add(s.next());
    			}
    		}
    
    		String[][] data = new String[oneDdata.size() / headers.size()][headers
    				.size()];
    		
    		System.out.println(headers.toArray());
    
    		for (int x = 0; x < oneDdata.size() / headers.size(); x++) {
    			for (int y = 0; y < headers.size(); y++) {
    				System.out.println(oneDdata.remove(0));
    			}
    		}
    
    
    	}
    
    
    	
    	public static void main(String[] str){
    		CSVparser c = new CSVparser();
    		try {
    			c.parse(new File("records.txt"));
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    }

  4. #4
    only_java is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    Use SuperCSV

    For more details you can see

    supercsv.sourceforge.net

  5. #5
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    I would, but I have to use my own.
    Please help
    I'm nearly there

  6. #6
    only_java is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default Have you tryied this !!!!

    In your code kindly remove that code marked with red :
    And Let me know this is what you want?

    for (int x = 0; x < oneDdata.size() / headers.size(); x++) {
    for (int y = 0; y < headers.size(); y++) {
    System.out.println(oneDdata.remove(0));
    }
    }

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    definately a problem with the way you tried to print it, not the parsing, read the above post, but also why are you removing the elements from the array

    oneDdata.remove(0)

    if you want to get the element without removing it do this:

    oneDdata.get(x)

  8. #8
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    ok thanks for the advice.
    But I've changed my code, as I need to do something else.
    here is my new code:
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    
    public class CSVparser {
    	public static void parse(File f) throws FileNotFoundException {
    		ArrayList<String> headers = new ArrayList<String>();
    		ArrayList<String> oneDdata = new ArrayList<String>();
    		Scanner lineScan = new Scanner(f);
    		Scanner s = new Scanner(lineScan.nextLine());
    		s.useDelimiter(",");
    		while (s.hasNext()) {
    			headers.add(s.next());
    		}
    
    		while (lineScan.hasNextLine()) {
    			s = new Scanner(lineScan.nextLine());
    			s.useDelimiter(",");
    			while (s.hasNext()) {
    				oneDdata.add(s.next());
    			}
    		}
    
    		String[][] data = new String[oneDdata.size() / headers.size()][headers
    				.size()];
    		
    		System.out.println(headers.toArray());
    
    
    		
    		
    		for (int y = 0; y < oneDdata.size()/headers.size(); y++) {
    			for(int x=0; x<headers.size(); x++){
    				data[y][x] = oneDdata.remove(0);
    				System.out.print(data[y][x] + ", ");
    				
    			}
    			System.out.print("\n");
    		}
    
    
    	}
    
    
    	
    	public static void main(String[] str){
    		CSVparser c = new CSVparser();
    		try {
    			c.parse(new File("records.txt"));
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    }
    The problem is that, I am trying to parse the following file:
    Java Code:
    col1, col2, col3, col4
    a1,a2,a3,a4
    b1,b2,b3,b4
    c1,c2,c3,c4
    d1,d2,d3,d4
    e1,e2,e3,e4
    and the output I am getting is:
    Java Code:
    a1, a2, a3, a4, 
    b1, b2, b3, b4, 
    c1, c2, c3, c4,
    I do not know where I am going wrong
    Please correct me
    Thanks

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    the answers are still there, please read them and try to implement them first.

  10. #10
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    are you sure? I'm really panicking with this :(
    I think there is a problem with parsing the file.
    But I really am not sure :(

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

Similar Threads

  1. help with parsing
    By ace_03 in forum New To Java
    Replies: 15
    Last Post: 11-24-2009, 10:02 PM
  2. Parsing XML
    By virvalid in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2009, 01:40 PM
  3. Xml Parsing
    By Nomad in forum XML
    Replies: 12
    Last Post: 02-22-2009, 12:19 PM
  4. xml parsing help
    By tankhardrive in forum XML
    Replies: 2
    Last Post: 02-12-2009, 12:45 AM
  5. xml parsing
    By gaurav65176 in forum XML
    Replies: 5
    Last Post: 11-12-2008, 12:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •