0

I'm trying to work with a String that is structured like this:

3882027944,Yes,"A specific website or app (please provide the answer)",
"Weather Underground, also local tv news half the time.",
"Somewhat likely","30 - 44", Male, "$10,000 to $24,999",
"West North Central"

(Linebreaks for clarity, not in the original text)

I've been trying to do this using the nextToken() function in Java. I'm trying to split the string by each part between the commas. However, if a String contains a comma (e.g. Weather Underground, also local...), it should not split there.

So my question is: Is there a way to split on a combination of tokens, so that I can split on "," instead of every seperate ,?

Erik S
  • 1,939
  • 1
  • 18
  • 44
  • 1
    Here is your solution : http://stackoverflow.com/questions/7800494/parse-csv-with-double-quote-in-some-cases – romfret May 27 '15 at 13:34
  • By the way, do you mean you use `StringTokenizer`? – Erik S May 27 '15 at 13:39
  • hmm.. thanks... but i don't think this is kind of the solutions i was looking for, i'm actually using trying to use tokenizer to read in bits and pieces of the whole string, as there are 100~1000 lines of similar data string, and using the pattern matching i don't think it will work the way i wanted to.. – t.chiang3100 May 27 '15 at 13:44
  • i thought nextToken(delimiter) also works? if i'm expecting different delimiter for each token... – t.chiang3100 May 27 '15 at 13:45

1 Answers1

0

You might want to use split instead of a tokenizer

From http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

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.

public static void main(String[] args) {
        String delims = "\",\"";
        String splitString = "one\",\"two,\",\"three\",\"four\",\",five";

        String[] tokens = splitString.split(delims);
        int tokenCount = tokens.length;
        for (int j = 0; j < tokenCount; j++) {
            System.out.println("Split Output: "+ tokens[j]);
        }
    }
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • yeah i guess this would be a better solution, i just thought there might be a way around using the tokenizers . Thanks. – t.chiang3100 May 27 '15 at 13:55