7

I'm reading a long line of info from a text file that looks exactly like this:

Sebastien 5000\\Loic 5000\\Shubhashisshh 5000\\Thibaullt 5000\\Caroo 5000\\Blabla 5000\\Okayyy 5000\\SebCed 5000\\abusee 5000\\omg 5000\\

It's supposed to be high scores with the names of users. When I print out the line, it looks exactly like it should, but when I print out the array after using split("\\\\"), it looks like this:

[Sebastien 5000, , Loic 5000, , Shubhashisshh 5000, , Thibaullt 5000, , Caroo 5000, , Blabla 5000, , Okayyy 5000, , SebCed 5000, , abusee 5000, , omg 5000]

The problem is that Array[0] is fine but Array[1] is empty, as are Array[3], Array[5], etc.

Here is my code. What's wrong with it?

            BufferedReader in = null;
    try {
        in = new BufferedReader(new FileReader(path));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    String line = null;
    try {
        line = in.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("LINE = "+line);

    String[] scores = line.split("\\\\");

    System.out.println("Mode = "+mode+Arrays.toString(scores));
Pops
  • 30,199
  • 37
  • 136
  • 151
user1895293
  • 115
  • 1
  • 13
  • Are you using `line.split("\\\\");` or is that a typo? I think you need `line.split("\\\");` – NickD Dec 11 '12 at 16:55
  • 1
    @NickD it takes a regex, so you need `"\\\\"` which first becomes a literal string ``\\`` and then a regex for a literal ``\``. `"\\\"` doesn't compile because it's unterminated string literal. – Esailija Dec 11 '12 at 16:57
  • It's not a typo, because you have to escape the \ once for a special char and a second time because it's a string if i want to split \\ i have to use \\\\ don't I? – user1895293 Dec 11 '12 at 17:02
  • Your code is working fine for me. – someone Dec 11 '12 at 17:06

3 Answers3

9

That's because "\\\\" is being parsed as \\ and the split method uses a regular expression, so \\ is becoming \, then Sebastien 5000\\Loic 5000 will result in [Sebastien 5000,,Loic 5000]

Do this instead: "\\\\\\\\"

Polyana Fontes
  • 3,156
  • 1
  • 27
  • 41
3

Just for fun, aside José Roberto solution, you can also use some alternative expressions (and lots others):

Two consecutive backslashes (same that in José's, but using a quantifier):

String[] scores = line.split("\\\\{2}");

Two consecutive Non-Word Characters:

String[] scores = line.split("\\W{2}");

Two consecutive punctuation chars:

String[] scores = line.split("\\p{Punct}{2}");

All of them produce the required output.

For more info on Regular expressions in Java:

Tomas Narros
  • 13,390
  • 2
  • 40
  • 56
0

I would have gone one further than Nick:

line.split("\\\");

This view assumes that you are trying to split the line at each point where the double back-slash appears - it seems like your code is splitting at every alternate double-slash, which would explain the double commas between each name; therefore, between each split part (i.e. each of the sections between the commas, there are two entries instead of one, so that one entry is simply a comma alone). See if this works - Good Luck!!

M.

Xavi López
  • 27,550
  • 11
  • 97
  • 161
advanceddriver
  • 21
  • 1
  • 1
  • 5
  • "\\" in java is single slash actually, and for regexp this is an escape character, so "\\\\" means single slash in regexp – hoaz Dec 11 '12 at 17:30