user2007843
user2007843

Reputation: 609

printing a split string

I am trying to print my string in the following format. ua, login, login ---> ua, navigation, fault Average = 500 milliseconds. I am storing the 2 strings into one string called keyString and putting it into the hashmap seperated by "|". I am then splitting that when I am iterating over the keyset to get it in the format I originally stated but it is showing up like this ---> ua, ctiq, export|ua, ctiq export, transfer Average = 600 milliseconds. Any ideas?

  public static void ProcessLines(Map<String, NumberHolder> uaCount,String firstLine, String secondLine) throws ParseException
   {
    String [] arr1 = firstLine.split("-- ", 2);
    String [] arr2 = secondLine.split("-- ", 2);
    String str1 = arr1[1];
    String str2 = arr2[1];


    ......
    String keyString = str1 + "|" + str2;               
    NumberHolder hashValue = uaCount.get(keyString);
     if(hashValue == null)
     {
        hashValue = new NumberHolder();
        uaCount.put(keyString, hashValue);                      
     }
     hashValue.sumtime_in_milliseconds += diffMilliSeconds;
     hashValue.occurences++;

     public static class NumberHolder
     {
      public int occurences;
      public int sumtime_in_milliseconds;       
     }

and heres the printing part

 for(String str : uaCount.keySet())
    {
        String [] arr = str.split("|",2);

        long average = uaCount.get(str).sumtime_in_milliseconds / uaCount.get(str).occurences;

        //System.out.println(str);

        System.out.println(arr[0] + " ---> " + arr[1] + " Average = " + average + " milliseconds");
    }

Upvotes: 1

Views: 92

Answers (1)

Pshemo
Pshemo

Reputation: 124265

split uses regular expression to match place to split, and in "RegEx" | means OR. To use | as literal you need to escape it with \ which in String is written as "\\". Alternatively you can use [|]. Try

str.split("\\|",2);

Upvotes: 1

Related Questions