0

My current problem is how you can read text files with java but that contain the pipes "|" And that in each separation I get the word that is contained in them.

Code To read now:

   while ((sCurrentLine = br.readLine()) != null) {
            String separado[] = sCurrentLine.split("|");
        for (int i = 0; i < sCurrentLine.length(); i++) {
            System.out.println(separado[i]);
        }

       // System.out.println(sCurrentLine);
    }

Text File Structure I Must Read:

Walther|28|M
Martha|28|F
Julio|28|M

The current result is:

W
a
l
t
h
e
r
|
2
8
|
M
M
a
r
t
h
a
|
2
8
|
F
J
u
l
i
o
|
2
8
|
M

I try to change the next line sCurrentLine.length() for this line separado.lenght But I got the following error cannot find symbol variable separado of type String[]

David
  • 347
  • 1
  • 3
  • 17

2 Answers2

0

You need to escape | in your split expression as that is a regular expression. Something like this would be better:

String separado[] = sCurrentLine.split("\\|");
Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
-1
while ((sCurrentLine = br.readLine()) != null) {
    String separado[] = sCurrentLine.split("\\|");
    for (int i = 0; i < separado.length; i++) {
        System.out.println(separado[i]);
    }
}
xlm
  • 6,854
  • 14
  • 53
  • 55
Aleksey Kurkov
  • 323
  • 2
  • 13