Results 1 to 6 of 6
  1. #1
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question Parsing a file with comma as delimiter, with some exceptions to it.

    Hi,


    I have a comma delimited Input file, each record has to be parsed.
    But some values have a double quotes surrounded them, as there is comma for that value.

    Example of the record is:
    LastName,FirstName,SSN,Street,County,State,Country
    "Andrew, Jr",Nick,1112223333,Pine View,Washington,Washington,US

    here in the above record, the LastName is "Andrew, Jr" and it is a single value.
    When I parse the record with comma as delimiter, the LastName is splitting into 2 values, whcih I dont want.

    How can i get the values in double quotes as single value and the rest be parsed with comma as delimier.

    Java Code:
    for(String input; (input = br.readLine()) != null;){
    			if(input.substring(0,4).equals("Last"))
    				//skip the firstLine and proceed, as it is the header line.
    				 continue;	
    			if(input.substring(0,2).equals(",,")) //skipping the lines which start with , (2 comma's)
    				continue;
    				recProvCount++;							
    			   String regx = "{}[]^+?`";   // dont want the record to contain the characters here.
    				    char[] ca = regx.toCharArray();
    				    for (char c : ca) {
    				        input = input.replace(""+c, ""); //replacing the chars with nothing.
    				    }
    				  input = input.replaceAll("[,*]", "~");  // tried changing the delimier comma to tilda. But that doesnot help me?
    				  System.out.println("The input line is: "+input);
    //				  input = input.substring(1);
    		
    				String[] column = input.split("~");
    				for(int i=0;i<=62;i++){
    					column[i] = column[i].toUpperCase(); //want the record to be uppercase.
    					column[i] = column[i].trim();//remove leading & trailing spaces from each field.
    					column[i] = column[i].replaceAll("\"+","");
    				}//end of for.
    Please help me, in solving the above problem. Thank You.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Parsing a file with comma as delimiter, with some exceptions to it.

    One approach is to count the number of double quotes. If the count is odd, then the comma is part of the string. If is even, the next comma is a delimiter. It could also be done using regular expressions.

    Regards,
    Jim
    Last edited by jim829; 07-05-2013 at 05:01 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    abhishekcs86 is offline Member
    Join Date
    Jul 2013
    Location
    Bangalore, India
    Posts
    3
    Rep Power
    0

    Default Re: Parsing a file with comma as delimiter, with some exceptions to it.

    Here you go.. Basically you can use Pattern and Matcher classes to replace commas occurring within quotes.
    Below example illustrates this...

    Java Code:
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class Help {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		String EXAMPLE_TEST = "\"Andrew, Jr\",Nick,1112223333,Pine View,\"Washington, dc\",Washington,US";
    		
    		Pattern replace = Pattern.compile("\"[a-zA-Z0-9 ]*[,][ a-zA-Z0-9]+\"");
    	    Matcher matcher = replace.matcher(EXAMPLE_TEST);
    	    
    	    
    	    while(matcher.find()){
    	    	String replaceWith = matcher.group().replaceAll(",", "!@@!");
    	    	EXAMPLE_TEST = EXAMPLE_TEST.replaceAll( matcher.group(), replaceWith);
    	    }
    	    System.out.println(EXAMPLE_TEST);
    	}
    }
    The output of this code when run on string

    "Andrew, Jr",Nick,1112223333,Pine View,"Washington, dc",Washington,US

    is

    "Andrew!@@! Jr",Nick,1112223333,Pine View,"Washington!@@! dc",Washington,US

    I just added one more comma within quotes to test :)

    Now easily run a String.split() over a comma and you should have your data..

    Happy coding..
    Last edited by abhishekcs86; 07-08-2013 at 11:17 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Parsing a file with comma as delimiter, with some exceptions to it.

    It is highly frowned upon to spoon feed solutions to the posters. The idea is to help them understand the problem and let them work it out themselves, offering assistance after they have tried some recommendations.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    abhishekcs86 is offline Member
    Join Date
    Jul 2013
    Location
    Bangalore, India
    Posts
    3
    Rep Power
    0

    Default Re: Parsing a file with comma as delimiter, with some exceptions to it.

    Ok...
    I didn't know that, as I am new to this forum. I shall ensure that I do not break the protocols from next time :)

    Thanks,
    Abhishek

    Quote Originally Posted by jim829 View Post
    It is highly frowned upon to spoon feed solutions to the posters. The idea is to help them understand the problem and let them work it out themselves, offering assistance after they have tried some recommendations.

    Regards,
    Jim

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Parsing a file with comma as delimiter, with some exceptions to it.

    Not to worry. And welcome to the forum!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Problems with loading a comma-seperated value file.
    By dddJewelsbbb in forum Advanced Java
    Replies: 25
    Last Post: 06-17-2012, 03:39 PM
  2. Reading a comma separated file into JTextPane
    By arndtmatt in forum New To Java
    Replies: 3
    Last Post: 04-09-2012, 10:08 PM
  3. MySQLDump to comma dilim text file
    By Sobutai in forum New To Java
    Replies: 1
    Last Post: 05-11-2011, 07:39 AM
  4. Delimite the file using comma
    By gokulcool in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 05:40 PM
  5. Replies: 7
    Last Post: 05-23-2008, 03:46 AM

Posting Permissions

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