I have this string which I am taking in from a text file.
"1 normal 1 [(o, 21) (o, 17) (t, 3)]"
I want to take in 1, normal, 1, o, 21, 17, t, 3
in a string array.
Scanner inFile = new Scanner(new File("input.txt");
String input = inFile.nextLine();
String[] tokens = input.split(" |\\(|\\)|\\[\\(|\\, |\\]| \\(");
for(int i =0 ; i<tokens.length; ++i)
{
System.out.println(tokens[i]);
}
Output:
1
normal
1
o
21
o
17
t
3
Why are there spaces being stored in the array.
That's not spaces, that's empty strings. Your string is:
"1 normal 1 [(o, 21) (o, 17) (t, 3)]"
It's split in the following way according to your regexp:
Token = "1"
Delimiter = " "
Token = "normal"
Delimiter = " "
Token = "1"
Delimiter = " "
Token = "" <-- empty string
Delimiter = "[("
Token = "o"
... end so on
When two adjacent delimiters appear, it's considered that there's an empty string token between them.
To fix this you may change your regexp, for example, like this:
"[ \\(\\)\\[\\,\\]]+"
Thus any number of " ()[,]"
adjacent characters will be considered as a delimiter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With