0

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

    }
2
  • It makes no sense to convert the String to a char array. The String class has convenient methods (like split()) that will do exactly what you need. Commented Jun 29, 2016 at 7:08
  • when i have used split() method than all indexes of array were mixed up and they are difficult to put in to list Commented Jun 29, 2016 at 7:12

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can you please explain me more that how can i use if/else in if(c.length() > 0) statement
1

May this code will help you

      import java.util.ArrayList;
      import java.util.List;

       public class MainClass {
public static void main(String[] args) {
   String input = "[:ordername:ordertime:orderprice:orderquantity:],                            [:ordername2:ordertime2:orderprice2:orderquantity2:]";

input = input.replace("[:", "");
input = input.replace(":]", "");

String[] inputArray = input.split(",");

List<String> nameList = new ArrayList<String>();
List<String> timeList = new ArrayList<String>();
List<String> priceList = new ArrayList<String>();
List<String> quantityList = new ArrayList<String>();

for (String order : inputArray) {
  String[] orderDetails = order.split(":");
  nameList.add(orderDetails[0]);
  timeList.add(orderDetails[1]);
  priceList.add(orderDetails[2]);
  quantityList.add(orderDetails[3]);
}
    }
    }

If you more concern about performance you can use apache commons for replacing string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.