Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2008

Tip: Looking for answers? Try searching our database.

parsing CSV data with quotation marks

Thread view: 
thufir - 08 May 2008 01:00 GMT
The text file really is CSV, but I also need to strip out the quotation
marks.  I suppose I can use the comma as a delimiter and then strip out
the quotation marks, but I feel like there's a better idiom.  Any
pointers?  (yes, I'll replace "foos" with guests.)

thufir@arrakis:~/bcit-comp2611-lab4$
thufir@arrakis:~/bcit-comp2611-lab4$
thufir@arrakis:~/bcit-comp2611-lab4$ cat src/a00720398/util/guests.txt
"Lee", "Brenda", "(604) 636-1000", "b.lee@bcit.ca"
thufir@arrakis:~/bcit-comp2611-lab4$
thufir@arrakis:~/bcit-comp2611-lab4$ cat src/a00720398/labs/Lab4.java
/**
* Lab4.java
*/

package a00720398.labs;

import java.util.*;
import java.io.*;
import a00720398.util.*;

public class Lab4 {
       public static void main (String[] args) {
               scan();
       }

       public static void scan(){
               Scanner s = null;
               List<String> guests = new ArrayList<String>();

               try {

                       //Scanner s = new Scanner(input).useDelimiter("\
\s*fish\\s*");
                       s = new Scanner(new BufferedReader(new FileReader
("guests.txt"))).useDelimiter("\"");
                       while (s.hasNext()) {
                               String guest = s.next();
                               guests.add(guest);
                       }
               } catch (IOException e) {
                       System.out.println(e.getMessage());
                       e.printStackTrace();
               } finally {
                       if (s != null) {
                               s.close();
                       }
               }

       System.out.println(guests);

       }
}

thufir@arrakis:~/bcit-comp2611-lab4$

thanks,

Thufir
Stefan Ram - 08 May 2008 01:11 GMT
>The text file really is CSV

 If this is an exercise, the teacher should
 give you a grammar for the language of the
 source file.

 The name »CSV« does not refer to a specific
 formal language or grammer.

 One can not write a scanner or a parser for
 a language that is not specified.
thufir - 08 May 2008 01:51 GMT
>   If this is an exercise, the teacher should give you a grammar for the
>   language of the source file.
[quoted text clipped - 4 lines]
>   One can not write a scanner or a parser for a language that is not
>   specified.

By grammar, this is where things are headed:

        firstName = "Brenda";
        lastName = "Lee";

but maybe I'm not using Scanner correctly because guests.get(0) is the
whole line, whereas I expected useDelimiter() to break it into smaller
pieces based on using a comma as the delimiter.  I'll re-read the Sun
tutorial, http://java.sun.com/docs/books/tutorial/essential/io/
scanning.html, but that's what I thought they were doing.

thufir@arrakis:~/bcit-comp2611-lab4$
thufir@arrakis:~/bcit-comp2611-lab4$ cat src/a00720398/labs/Lab4.java
/**
* Lab4.java
*/

package a00720398.labs;

import java.util.*;
import java.io.*;
import java.util.ArrayList;
import a00720398.util.*;
import a00720398.data.*;

public class Lab4 {
       public static void main (String[] args) {
//              scan();
//      }

//      public static void scan(){
               Scanner s = null;
               ArrayList<String> guests = new ArrayList<String>();

               try {

                       //Scanner s = new Scanner(input).useDelimiter("\
\s*fish\\s*");
                       s = new Scanner(new BufferedReader(new FileReader
("guests.txt"))).useDelimiter("/,");
                       while (s.hasNext()) {
                               String guest = s.next();
                               guests.add(guest);
                       }
               } catch (IOException e) {
                       System.out.println(e.getMessage());
                       e.printStackTrace();
               } finally {
                       if (s != null) {
                               s.close();
                       }
               }

       System.out.println(guests.get(0));

       }
}

thufir@arrakis:~/bcit-comp2611-lab4$

thanks,

Thufir
thufir - 08 May 2008 02:15 GMT
> but maybe I'm not using Scanner correctly because guests.get(0) is the
> whole line, whereas I expected useDelimiter() to break it into smaller
> pieces based on using a comma as the delimiter.  I'll re-read the Sun
> tutorial, http://java.sun.com/docs/books/tutorial/essential/io/
> scanning.html, but that's what I thought they were doing.

Well, the example at http://www.kodejava.org/examples/242.html solved my
problems.  I'm not quite sure *why*, but it looks like they're
differentiating lines.

-Thufir
Roedy Green - 08 May 2008 20:26 GMT
>firstName = "Brenda";

that is not what is usually meant by CSV.  That looks more like a
subset of Java.
see http://mindprod.com/jgloss/csv.html
http://mindprod.com/jgloss/parser.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 08 May 2008 20:13 GMT
>The text file really is CSV, but I also need to strip out the quotation
>marks.

see http://mindprod.com/jgloss/csv.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.