2012-02-12 55 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)获得价值。