Search code examples
javaarrayliststring-parsing

How to parse the string in to different arraylist of string type


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]);
        }

    }

Solution

  • Something like this will work:

    Map <Integer, List <String>> map = new HashMap <>();
    
    // Initialize the map
    map.put(1, new ArrayList <String> ());
    map.put(2, new ArrayList <String> ());
    map.put(3, new ArrayList <String> ());
    map.put(4, new ArrayList <String> ());
    
    String str = "[:ordername:ordertime:orderprice:orderquantity:]," + 
                 "[:ordername2:ordertime2:orderprice2:orderquantity2:]";
    
    // loop through each order set
    for (String s: str.split(","))
    {
        // remove any leading and trailing spaces
        s = s.trim();
    
        // remove the brackets 
        s = s.replaceAll("[\\[\\]]", "");
    
        int i = 1;
    
        // loop through each order component
        for (String c: s.split(":"))
        {
            // remove any leading and trailing spaces
            c = c.trim();
            if (c.length() > 0)
            {
                map.get(i).add(c);
                i++;
            }
        }
    }
    
    System.out.println(map);
    

    Outputs:

    {1=[ordername, ordername2], 2=[ordertime, ordertime2], 3=[orderprice, orderprice2], 4=[orderquantity, orderquantity2]}

    Note: For simplicity, I'm using a HashMap to contain all the Lists, but you can create the 4 lists outside of a HashMap if you wish. In that case, you would need to have if/else if conditions in the if (c.length() > 0) statement.