1

I am trying to update a HashMap use it directly in the next method, but it isn't working. From what I read I couldn't find a solution. Some say'd it is impossible and some say use an iterator, but even with the iterator it's not working. the error is the printing method it is not printing or even getting inside the while loop because it is empty but i cant find why

This is the two methods I'm trying to update and print some information.

  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);
        }
    }
}
3
  • Your code doesn't contain a map at all, which makes it somewhat tricky to help you. Please read tinyurl.com/so-hints Commented Feb 12, 2012 at 21:33
  • sorry i forgot to put the code for the map Commented Feb 12, 2012 at 21:34
  • You also forgot to tell us WHAT it is that's not working... Commented Feb 12, 2012 at 21:45

2 Answers 2

2

You're probably getting a NullPointerException? Next time tell us what is going wrong and provide stacktraces if applicable.

The code you posted doesn't create an instance of orderList, so if it's not done elsewhere that code will throw a NullPointerException

Try adding:

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

Swallowing an Exception like this:

} catch (Exception e) {
}

is not a good practice since it will hide all information about what's going wrong, at least do:

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

}

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

3 Comments

okay i change the code and it is still not working or showing me any errors
Still kind of hard to help you more since you haven't specified WHATs not working. How are you calling your methods for example?
I tried the following: OrderList l = new OrderList(); l.getSplitLinesOrders("PETER,213,123,123"); System.out.println( l.listDetails()); and it produced "Order@71060478" (I had to create an Order class since it was missing in your code). What's the scenario NOT working
0

You could do something like this:

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

Which is the initialization of the iterator, and then you could iterate through the keys using i.next(), getting a String each time and asking orderList.get(thatString) to get the value.

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.