I have string which contains 4 things and i want to store each thing in a list. The string contains food orders which is coming from android to server, and in server i have to parse it to display. The string looks like:
[:ordername:ordertime:orderprice: orderquantity:]
For 1 order case I want to enter ordername in name list and ordertime in time list and orderprice in pricelist and so on If more than 1 order will come it will be separated by commas like
[:ordername:ordertime:orderprice:orderquantity:],[:ordername2:ordertime2:orderprice2:orderquantity2:]
I want to enter ordername,ordername2 in name list and ordertime, ordertime2 in time list and orderprice , orderprice2 in price list and so on.
this is what i have tried
String orderlist=request.getParameter("orderlist"); // this is a string which is coming from android to server containing orders
char[] orderarray=orderlist.toCharArray(); //converting string to char array
int comma_counter = 0;
int comma_counter1 = 0;
for (int i=0; i < orderlist.length(); i++){
if (orderarray[i]==','){
comma_counter++;
}
}
System.out.println("counter is"+comma_counter);
System.out.println("order list length"+orderlist.length());
ArrayList <String> order_array_list = new ArrayList <String>();
int no=0;
String temp="";
for (int j=no; j<orderarray.length; j++){
System.out.println(" j is "+ j);
if(orderarray[j]!=','){
temp = temp+orderarray[j];
// System.out.println("temp is "+temp);
}
else if(orderarray[j]==','){
order_array_list.add(temp);
temp="";
no=j;
}
}
String []parts= null;
for(int i=0; i<order_array_list.size(); i++){
String array= order_array_list.get(i);
parts= array.split(":");
for(int j=0; j<parts.length; j++)
{
System.out.println(parts[j]);
}
}
Stringto achararray. TheStringclass has convenient methods (likesplit()) that will do exactly what you need.