Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

parsing each line in text file java

I have a text file that has the following lines:

150004|2012|12|15|0|0|3|0|0|-3.2411|83.9962|156.3321|1.1785|205.3125|2.0599
150004|2012|12|15|0|10|3|0|0|-3.4206|85.9575|150.4877|1.4142|226.7578|2.4276
150004|2012|12|15|0|20|3|0|0|-2.2696|86.2675|149.3848|2.1553|225.7031|3.4387

every '|' sign indicates it has a column. I have to extract the info from each line that is inside of '|' signs. When I try the following code:

File filer = new File("C:\Users\Ali Y. Akgul\Desktop\150004_15122012_G.txt");
        try (BufferedReader reader = new BufferedReader(new FileReader(filer))) {
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                String[] fields = line.split("|");
                // process fields here
                for(int i=0;i<=fields.length;i++){
                    System.out.println(fields[i]);
                }
            }
        }
}

it gives me:

1
5
0
0
0
4
|
2
0
1
2
|
1
2
|
1
5
|
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 76
0
|
0
|
3
|
0
|
0
|
-
3
.
2
4
    at testenv.TestEnv.main(TestEnv.java:31)
1
1
|
8
3
.
9
9
6
2
|
1
5
6
.
3
3
2
1
|
1
.
1
7
8
5
|
2
0
5
.
3
1
2
5
|
2
.
0
5
9
9
Java Result: 1

How can I parse it correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is because that String.split uses a regex.

In regexes, the | character is a special character meaning either the pattern on the left OR on the right of the character. It has to be escaped with a backslash (\)

The correct syntax is:

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

Also, take nto that I didn't see the issue with the for loop, but that needs fixing too, that is why the ArrayOutOfBoundsException pops up its ugly head...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...