2016-06-29 12 views
0

私は4つのものを含む文字列を持っており、それぞれのものをリストに格納したいと思います。 文字列にはアンドロイドからサーバーに送信された料理の注文が含まれています。サーバーでは、これを解析して表示する必要があります。 文字列のようになります。私は、1つの以上の順序は、それが区切られます来る場合は価格表にし、そう に時間リストとorderpriceで名前リストとordertimeでORDERNAMEを入力したい1つの注文の場合について文字列型の別のarraylistに文字列をパースする方法

[:ordername:ordertime:orderprice: orderquantity:] 

[:ordername:ordertime:orderprice:orderquantity:],[:ordername2:ordertime2:orderprice2:orderquantity2:] 

のようなコンマは私は上の価格表内の名前リストとordertimeordernameordername2、時間リストとorderpriceordertime2orderprice2を入力したいです。

これは私はこのような何かが動作する

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

    } 
+0

'String'を' char'配列に変換するのは意味がありません。 'String'クラスには便利なメソッド(' split() 'など)があり、必要なものを正確に処理します。 –

+0

配列のすべてのインデックスよりもsplit()メソッドを使用していて、リストに配置するのが難しい場合 –

答えて

2

を試みたものである。

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

出力

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

:簡単にするために、私はすべてのリストを含むようにHashMapを使用していますが、あなたが望むなら、あなたはHashMapの外に4つのリストを作成することができます。その場合、if (c.length() > 0)ステートメントにif/else ifの条件が必要です。

+0

あなたは私にif/elseをif(c.length()> 0)ステートメント –

1

このコードは、文字列を置換するためのApacheのコモンズを使用することができ、パフォーマンスについてあなたより懸念した場合、あなたに

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

を助ける可能性があります。

関連する問題