1

I have a text file with two pieces of data:

1675 | 28/02/2015 | Jane Smith | James C | James C | 4 | 3

My is as follows:

      while ((line = br.readLine()) != null) {
            String parts[] = line.split(" | ");
            String cID = parts[0];
            String cDate = parts[1];
            String cName = parts[2];
            String cName2 = parts[3];
            String wName = parts[4];
            String s1 = parts[5];
            String s2 = parts[6];
            //System.out.println(cID + cDate + cName + cName2 + wName + s1 +                   s2);
            System.out.println(Arrays.toString(parts));   
        }
        br.close(); 

However I get the following error:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1

JSmithers
  • 83
  • 8
  • 4
    `String.split(regex)` is using a regular expression to find the split indices and thus you need to escape your pipe character, i.e. `" \\| "`. – Thomas Mar 02 '16 at 09:25

1 Answers1

1

You can try this to split the string with |

str.split("\\|")

Put \\ before |

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68