2

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?

7 Answers 7

8

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...

Sign up to request clarification or add additional context in comments.

Comments

5
for(int i=0;i<=fields.length;i++) 

needs to be

for(int i=0;i<fields.length;i++)

(The '<=' must be '<')

Also the issue with your regexp pointed out in other answers.

Comments

3

| is a special character in regex which acts an OR operator, you'll need to escape the expression using:

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

Comments

3

Instead of for(int i=0;i<=fields.length;i++){ use for(int i=0;i<fields.length;i++){ so use < instead of <= in the condition.

1 Comment

and consider to comment of @ppeterka
2

Try this:

Path file = Paths.get("C:\\Users\\Ali Y. Akgul\\Desktop\\150004_15122012_G.txt");

ArrayList<String> lines = Files.readAllLines(file, Charset.defaultCharset());
ArrayList<String []> columns = new ArrayList<>();
for(String line : lines){
    columns.add(line.split('\|'));
}

// Now for each line you have columns.
for(String [] s : columns){ 
    System.out.println(Arrays.toString(s));
}

// To get only the values for column 8 onwards (in response to your comment)
for(String [] s : columns){ 
    String [] sublist = Arrays.copyOfRange(s, 8, s.length);
    System.out.println(Arrays.toString(sublist));
}

// To get only the columns from line 8 onwards
for(int i = 0; i < columns.size(); i++){
    System.out.println(Arrays.toString(columns.get(i)));
}        

7 Comments

Thanks a lot for your solution. your solution gives like [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] Now how to get the columns from 8 to last?
Do you want to drop the first 7 values of each array? Or do you want to drop the first 7 lines?
well I will use them too. but necessary part is from 8 to last. I have to assign such strings for each, so that I can save them on database.
I changed columns[i] to columns.get(i).toString() because it says array required but arraylist found. THrough my way it gives like [Ljava.lang.String;@6e677ea2 [Ljava.lang.String;@4caaf64e
nope it still says, array required but arraylist<String[]> found
|
2

It seems that you have a boundary issue in the following lines:

for(int i=0;i<=fields.length;i++){
   System.out.println(fields[i]);
}

should be

for(int i=0;i<fields.length;i++){
   System.out.println(fields[i]);
}

Comments

1

shoud be smaller than: for(int i=0;i<fields.length;i++)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.