How to use both a new line and comma as a delimeter for the StringTokenizer

91 Views Asked by At

I want to be able to use both the comma and a new line as a delimeter for tokens. This would be for just commas:Tokens = new StringTokenizer(line,","); but how would I implement it to account for a new line as well?

I tried ",\n"and \n," but they do not work.

I am trying to extract the data from a text file that looks like this: hello apple, tree, wolf eagle badge

1

There are 1 best solutions below

2
BIBOO nation On

I tested the following on an online java compiler.

import java.util.StringTokenizer;

public class MyClass {
    public static void main(String args[]) {
        String yourString = "a , string ,\n with \n commas, and \n, newlines";
        StringTokenizer tokenizer = new StringTokenizer(yourString, ",\n");
        
        while (tokenizer.hasMoreTokens()){
            System.out.println("token: (" + tokenizer.nextToken()+"),");  
        }
    }
}

The output I received:

token: (a ),
token: ( string ),
token: ( with ),
token: ( commas),
token: ( and ),
token: ( newlines),

The example parses the tokens for ,, ,\n, \n, and \n Though if it really doesn't work for you, try using Split() as the link shown in the comments suggests as it allows regex to only allow ,\n or \n,