Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confused how .split() works in Java

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.

like image 285
noobstudent Avatar asked Oct 13 '15 08:10

noobstudent


1 Answers

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.

like image 85
Tagir Valeev Avatar answered Sep 30 '22 09:09

Tagir Valeev