2011-05-11 44 views
3

美好的一天!如何將字符串解析爲多個數組

我想提出一個小書店計劃,我們需要閱讀哪些客戶在指定櫃檯購買基於文件如下:

counter 4,book1 2,book2 2,book3 2,tender 100.00 
counter 1,book1 2,book2 1,book3 3, book4 5,tender 200.00 
counter 1,book3 1,tender 50.00 

總之格式爲: COUNTER - >產品買 - >投標

我想這樣做,但它不是有效的:

public List<String> getOrder(int i) { 
     List <String> tempQty = new ArrayList<String>(); 
     String[] orders = orderList.get(0).split(","); 
     for (String order : orders) { 
      String[] fields = order.split(" "); 
      tempQty.add(fields[i]); 
     } 
     return tempQty; 
} 

我怎樣才能讀取該文件,並在同一時間,確保我將它放在正確的數組上?除了櫃檯和招標書之外,我需要知道書的名稱和數量,以便我能夠得到它的價格和計算機的總價。我需要做多個數組來存儲每個值嗎?任何建議/代碼將是 高度讚賞。

謝謝。

+0

注意在'招標'之前的空間在第二行。 – 2011-05-11 13:33:07

+0

糾正它。謝謝 – newbie 2011-05-11 13:36:57

回答

2
Map<String, Integer> itemsBought = new HashMap<String, Integer>(); 
    final String COUNTER = "counter"; 
    final String TENDER = "tender"; 
    String[] splitted = s.split(","); 
    for (String str : splitted) { 
     str = str.trim(); 
     if (str.startsWith(COUNTER)) { 
      //do what you want with counter 
     } else if (str.startsWith(TENDER)) { 
      //do what you want with tender 
     } else { 
      //process items, e.g: 
      String[] itemInfo = str.split(" "); 
      itemsBought.put(itemInfo[0], Integer.valueOf(itemInfo[1])); 
     } 
    } 
1

這個怎麼樣?在這裏,我們有一個模擬購買建模的課程,其中包含計數器,投標和已購物品清單。購買的商品由一個id(例如book1)和一個數量組成。使用方法readPurchases()來讀取文件的內容並獲取購買清單。

這是否解決您的問題?

public class Purchase { 
    private final int counter; 
    private final List<BoughtItem> boughtItems; 
    private final double tender; 

    public Purchase(int counter, List<BoughtItem> boughtItems, double tender) { 
     this.counter = counter; 
     this.boughtItems = new ArrayList<BoughtItem>(boughtItems); 
     this.tender = tender; 
    } 

    public int getCounter() { 
     return counter; 
    } 

    public List<BoughtItem> getBoughtItems() { 
     return boughtItems; 
    } 

    public double getTender() { 
     return tender; 
    } 
} 

public class BoughtItem { 
    private final String id; 
    private final int quantity; 

    public BoughtItem(String id, int quantity) { 
     this.id = id; 
     this.quantity = quantity; 
    } 

    public String getId() { 
     return id; 
    } 

    public int getQuantity() { 
     return quantity; 
    } 
} 

public class Bookstore { 

    /** 
    * Reads purchases from the given file. 
    * 
    * @param file The file to read from, never <code>null</code>. 
    * @return A list of all purchases in the file. If there are no 
    *   purchases in the file, i.e. the file is empty, an empty list 
    *   is returned 
    * @throws IOException If the file cannot be read or does not contain 
    *      correct data. 
    */ 
    public List<Purchase> readPurchases(File file) throws IOException { 

     List<Purchase> purchases = new ArrayList<Purchase>(); 

     BufferedReader lines = new BufferedReader(new FileReader(file)); 
     String line; 
     for (int lineNum = 0; (line = lines.readLine()) != null; lineNum++) { 

      String[] fields = line.split(","); 
      if (fields.length < 2) { 
       throw new IOException("Line " + lineNum + " of file " + file + " has wrong number of fields"); 
      } 

      // Read counter field 
      int counter; 
      try { 
       String counterField = fields[0]; 
       counter = Integer.parseInt(counterField.substring(counterField.indexOf(' ') + 1)); 
      } catch (Exception ex) { 
       throw new IOException("Counter field on line " + lineNum + " of file " + file + " corrupt"); 
      } 

      // Read tender field 
      double tender; 
      try { 
       String tenderField = fields[fields.length - 1]; 
       tender = Double.parseDouble(tenderField.substring(tenderField.indexOf(' ') + 1)); 
      } catch (Exception ex) { 
       throw new IOException("Tender field on line " + lineNum + " of file " + file + " corrupt"); 
      } 

      // Read bought items 
      List<BoughtItem> boughtItems = new ArrayList<BoughtItem>(); 
      for (int i = 1; i < fields.length - 1; i++) { 
       String id; 
       int quantity; 
       try { 
        String bookField = fields[i]; 
        id = bookField.substring(0, bookField.indexOf(' ')); 
        quantity = Integer.parseInt(bookField.substring(bookField.indexOf(' ') + 1)); 

        BoughtItem boughtItem = new BoughtItem(id, quantity); 
        boughtItems.add(boughtItem); 
       } catch (Exception ex) { 
        throw new IOException("Cannot read items from line " + lineNum + " of file " + file); 
       } 
      } 

      // We're done with this line! 
      Purchase purchase = new Purchase(counter, boughtItems, tender); 
      purchases.add(purchase); 
     } 

     return purchases; 
    } 
}