Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By masijade

Thread: Reading from TreeMap, program always throws an exception

  1. #1
    pokadean is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default Reading from TreeMap, program always throws an exception

    I have the following program I created:
    A program where at a certain time i want to find out ALL the products some certain client bought.
    I store my clientOrders in a Map using a String specifying the type of order.
    And when I want to retrieve all the orders of a Client (where the client code is inside every single entry of my map) I just do so:
    Java Code:
    Map<String,ClientOrder> orders = new TreeMap<String,ClientOrder>();
    
    //...
    
    public ArrayList<ClientOrder> getClientOrders(String clientCode)
                    throws MissingCode {
    ArrayList<ClientOrder> orderss= new ArrayList<ClientOrder>();
      int i=0;
        	for(String g: orders.keySet()){                     // is this right? it iterates on the keys of the Map, right?
            String l= orders.get(g).getClientCode();         // then for every entry of my Map i get the ClientCode using the method
        	if (l.equals(clientCode)){                              // and look for its equality with the parameter passed to the method
            orderss.add(orders.get(g));                      // if they're equal I add the Map row to the ArrayList and change i
            i=1;
        	}
        	}
        	if (i==0) throw new MissingCode();
        	return orderss;
        }
    So basically I to a temporary arraylist the rows of my map where i find that my ClientCode is the one I'm looking for, using the method getClientCode() that IS working because I'm already using it in other methods and I have no problems.
    The program obviously isn't changing the value of i to 1 and so it throws the exception.
    Do you guys think there's something i'm missing?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Reading from TreeMap, program always throws an exception

    First, why use an int rather than a boolean. Secondly, why not also either
    1) create a second map with ClientOrder as the key and these Strings as the values (i.e. a reverse lookup map)
    or
    2) create a field within your ClientOrder class to hold this String value
    and, in both cases, avoid this loop all together.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Reading from TreeMap, program always throws an exception

    @masijade

    1) would have to be a List of Strings as the value of the Map then

    2) I think in this case you also mean to add the List of Strings (client codes) to the ClientOrder class, but I'm not sure
    masijade likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Reading from TreeMap, program always throws an exception

    @gimbal2

    Yes, that's correct, of course, if it can actually be more than one.

    Edit: But, in that case, I, personally, would use a Set, and not a List.
    Last edited by masijade; 09-13-2013 at 09:54 AM.
    gimbal2 likes this.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Reading from TreeMap, program always throws an exception

    Highly agreed on using a Set.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. The below program fails and throws an exception
    By frintocf in forum New To Java
    Replies: 1
    Last Post: 10-04-2011, 09:41 AM
  2. throws exception
    By simorgh in forum New To Java
    Replies: 1
    Last Post: 07-30-2010, 12:24 AM
  3. Main method with throws Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 02:48 PM
  4. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •