2012-02-12 51 views
1

我試圖更新HashMap直接在下一個方法中使用它,但它不起作用。從我讀的東西我找不到解決方案。有人說這是不可能的,有人說使用迭代器,但即使使用迭代器也不行。錯誤是打印方法,它不打印,甚至進入while循環,因爲它是空的,但我找不到爲什麼Java HashMap創建,編輯和刪除

這是我試圖更新和打印一些信息的兩種方法。

import java.io.File; 
    import java.util.ArrayList; 
    import java.util.HashMap; 
    import java.util.Iterator; 
    import java.util.Scanner; 
    import java.util.Enumeration; 
    import java.util.Hashtable; 
    import java.util.Iterator; 
    import java.util.Map; 
    import java.util.Set; 

    public class OrderList { 
    // Storage for an arbitrary number of details. 

    private HashMap<String, Order> orderList = new HashMap<String, Order>(); 

    /** 
    * Perform any initialization . 
    */ 
    public OrderList() { 
     orderList = new HashMap<String, Order>(); 
    } 

    public HashMap<String, Order> getOrders() { 
     return orderList; 

    } 

    public void readOrderFile(String OrderListPath) { 
     try { 
      File file = new File(OrderListPath); 
      Scanner scan = new Scanner(file); 
      while (scan.hasNextLine()) { 
       String readLine = scan.nextLine(); 

       if (readLine != null) { 

        getSplitLinesOrders(readLine); 
       } 
      } 

     } catch (Exception e) { 
     } 
    } 

    public void getSplitLinesOrders(String readLine) { 
     String id = ""; 
     String customerId = ""; 
     String itemId = ""; 
     int quantity = 0; 
     try { 
      String[] splitedLine = readLine.split(","); 

      if (splitedLine.length == 4) { 
       id = splitedLine[0]; 
       customerId = splitedLine[1]; 
       itemId = splitedLine[2]; 
       quantity = Integer.parseInt(splitedLine[3]); 
       Order newOrder = new Order(id, customerId, itemId, quantity); 
       orderList.put(id, newOrder); 
      } 
     } catch (Exception e) { 
     } 


    } 

    /** 
    * Add a new set of details to the list 
    * @param details The details of the staff 
    */ 
// public void addDetails(Order details) { 
//  orderList.add(details); 
// } 
    public boolean hasOrder() { 
     return orderList.size() != 0; 
    } 

    public Order getNextOrder() { 
     Order order = orderList.remove(0); 
     return order; 

    } 

    /** 
    * @return All the details 
    */ 
    public String listDetails() { 
     StringBuffer allEntries = new StringBuffer(); 
     for (Map.Entry<String, Order> details : orderList.entrySet()) { 
      String Key = details.getKey(); 
      Object value = details.getValue(); 
      allEntries.append(Key + " " + value); 
     } 
     return allEntries.toString(); 
    } 

    public void PrintListOfOrders() { 
     Iterator it = getOrders().entrySet().iterator(); 
     try { 

      while (it.hasNext()) { 
       Order value = (Order) it.next(); 
       System.out.println(value.getOrderId() + " " + value.getCustomerId() + " " +  value.getItemId() + " " + value.getQuantity()); 
      } 




     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 
} 
+0

您的代碼根本不包含地圖,這對您有所幫助。請閱讀http://tinyurl.com/so-hints – 2012-02-12 21:33:22

+0

對不起,我忘了把地圖的代碼 – 2012-02-12 21:34:32

+0

你也忘了告訴我們這是不是工作... – 2012-02-12 21:45:12

回答

2

您可能會收到NullPointerException?下次告訴我們什麼是錯誤的,並提供堆棧跟蹤(如果適用)。

您發佈不創建的orderList一個實例,因此,如果它沒有做過的代碼會拋出一個NullPointerException

嘗試添加代碼:

private HashMap<String, Order> orderList = new HashMap<String, Order>; 

Exception這樣的:

} catch (Exception e) { 
} 

不是一個好的做法,因爲它會隱藏所有有關錯誤的信息,至少可以這樣做:

catch (Exception e) { 
    e.printStacktrace(); 

}

+0

好吧我改變了代碼,它仍然是不工作或向我顯示任何錯誤 – 2012-02-12 22:00:23

+0

由於您沒有指定什麼不起作用,所以仍然很難幫助您。你如何調用你的方法? – 2012-02-12 22:05:41

+0

我試過以下內容:OrderList l = new OrderList(); l.getSplitLinesOrders(「PETER,213,123,123」); System.out.println(l.listDetails());它產生了「Order @ 71060478」(我不得不創建一個Order類,因爲它在代碼中缺失)。什麼情況下不起作用 – 2012-02-12 22:09:45

0

你可以做這樣的事情:

Set<String> s = List.keySet(); 
Iterator<String> i = s.iterator(); 

這是迭代器的初始化,然後你可以通過使用i.next()按鍵迭代,每次得到一個字符串並要求orderList.get(thatString)獲得價值。