noobstudent
noobstudent

Reputation: 59

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.

Upvotes: 5

Views: 111

Answers (2)

hata
hata

Reputation: 12478

For example here:

1 [(o
  1. At first step it matches a single space.
  2. The next step it matches [(

So between these two matching, a void String "" is returned.

Upvotes: 0

Tagir Valeev
Tagir Valeev

Reputation: 100169

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.

Upvotes: 6

Related Questions