-1

I was expecting the OUTPUT to be 3 from the below class:

public class Pattern {
    public static void main(String[] args) {
        String data = "111,577,5099,541,142,
                       2015-08-01 00:08:42,2015-08-01 06:31:52|
                       674,898,7061,36,105,2015-08-01 19:28:45,
                       2015-08-02 14:46:27|948,522,1840,66,889,
                       2015-08-02 13:04:56,2015-08-02 19:39:57";

        if(data.contains("|"))
        {
           String pattern[] = data.split("|");
           System.out.println("the pattern length:  "+pattern.length);
        }
    }
}

OUTPUT:

the pattern length: 180

sjain
  • 23,126
  • 28
  • 107
  • 185

2 Answers2

4

The character | is a special character in regexes (the split method uses regexes)

You'll have to go with

String pattern[] = data.split("\\|");
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
1

kindly update your code likewise,

 String pattern[] = data.split("\\|");
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55