Results 1 to 2 of 2
  1. #1
    lulzim is offline Member
    Join Date
    Feb 2011
    Rep Power

    Default I need help while reading the CSV file

    I am trying to process the CSV file and storing the data into the ArrayList
    Some of the columns has got a string which could consist of "comma" character,

    So how can I deal for example:
    "Curative (acute) care beds, density per 1 000 population",2000,Australia,3.6, so i want to avoid this "," character in the first "Curative (acute) care beds, density per 1 000 population" i want this to insert as one token in arraylist , but my code is taking as two tokens.
    XML Code:
    public  void readfile(String filename) throws IOException
    	String strData;
    	int no_lines=0;
    	BufferedReader bf= new BufferedReader(new FileReader(filename));
    	String line = null;
    	String strLine = "";
    	StringTokenizer st = null;
    	int tokenNumber = 0;
    	int count;
    		Element e = new Element();
    	count = 1;	
    //	System.out.println(strLine);
    	st = new StringTokenizer(strLine, "\"");
    //	System.out.println("The data from file " + strLine);
    		//display csv values
    			strData =	 st.nextToken();
    //			System.out.println("The data from file " + strData);
    //				System.out.println("The data from file " + strData);
    				e.settype(strData); // thirrim reference e per ta thirrru metodet setup e cila e merr vetem type series dhe pastaj i fusim me nje arraylist
    			else if(count==2){
    			else if (count==3){
    			else if (count==4){
    			else {}

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    Loop through the line using the ',' token delimiter, but check each token to see if it starts with ". If it does, start an inner loop and concatenate subsequent tokens to the first until a token ends with ". For example:
    Java Code:
    private String getCSVField(Iterator tokenIter, String delimiter) {
        if (!tokenIter.hasNext()) {
            return null;
        StringBuffer fieldBuf = new StringBuffer();
        String token = (String);
        if (token.startsWith(DBL_QUOTE)) {
            while (!token.endsWith(DBL_QUOTE)) {
                fieldBuf.append(Strings.trimString(token, DBL_QUOTE));
                fieldBuf.append(delimiter);  // add the delimiter back - it's part of the field
                token = (String);
            token = Strings.trimString(token, DBL_QUOTE);
        return fieldBuf.toString();
    This version assumes a standard Iterator for the tokens, and removes the double-quotes from the field.

    Incidentally, "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead". See the StringTokenizer API docs.
    Last edited by dlorde; 05-26-2011 at 12:19 AM.

Similar Threads

  1. Reading file external to jar file
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 02-04-2011, 05:46 AM
  2. reading a file and writing to a!!!!
    By java_prgr in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 06:53 PM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Reading XML from file
    By chriscant in forum New To Java
    Replies: 0
    Last Post: 03-17-2009, 01:34 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