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
  • 325
  • 1
  • 2
  • 15

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,699
  • 52
  • 35
  • 51
-1
while ((sCurrentLine = br.readLine()) != null) {
    String separado[] = sCurrentLine.split("\\|");
    for (int i = 0; i < separado.length; i++) {
        System.out.println(separado[i]);
    }
}
xlm
  • 5,564
  • 13
  • 48
  • 52
Aleksey Kurkov
  • 238
  • 2
  • 12