java - printing a split string -
i trying print string in following format. ua, login, login ---> ua, navigation, fault average = 500 milliseconds
. storing 2 strings 1 string called keystring
, putting hashmap seperated "|"
. splitting when iterating on keyset in format stated showing ---> ua, ctiq, export|ua, ctiq export, transfer average = 600 milliseconds
. 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 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"); }
split
uses regular expression match place split, , in "regex" |
means or. use |
literal need escape \
in string written "\\"
. alternatively can use [|]
. try
str.split("\\|",2);
Comments
Post a Comment