-1

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

  • Will [this link](https://stackoverflow.com/a/9446740/16034206) help? Though you say that what you tried doesnt work, but I have no idea how it does not work. – experiment unit 1998X Jan 12 '23 at 01:18
  • Using `",\n"` or `"\n,"` should work. For us to determine what went wrong, we need more information. We need to be able to duplicate the problem. Please edit your question to show your code as a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Old Dog Programmer Jan 12 '23 at 01:25
  • I am reading data off a text file that just has words on separate lines. Some lines have one word only while others have multiple words separated by commas. My goal is to extract the data even though there are two delimeters: a comma and a new line – CrustyCrustecean Jan 12 '23 at 01:32
  • @CrustyCrustecean, since `tokens = new StringTokenizer(line,",/n");` should work, the problem might be elsewhere. We need to see complete, relevant code. So, you need to show us a *minimal reproducible example*. Without it, we can't really help. – Old Dog Programmer Jan 12 '23 at 02:10
  • 2
    No need to use legacy class `StringTokenizer` which is not in any case for file parsing really. Use `s = new Scanner(file).useDelimiter("[, \r\n]+);` and you can do `word = s.next()` through the whole file – g00se Jan 12 '23 at 10:32

1 Answers1

1

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,

  • I am reading data off a text file that just has words on separate lines. Some lines have one word only while others have multiple words separated by commas. My goal is to extract the data even though there are two delimeters: a comma and a new line – CrustyCrustecean Jan 12 '23 at 01:32
  • What are you using to read data off the textfile? If you are using a Scanner, there probably is no newline character, as Scanner already handles it for you using `Scanner.nextLine()` – experiment unit 1998X Jan 12 '23 at 01:34