Results 1 to 2 of 2

Thread: Issue in code for changing an item quantity (with do/while)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    97

    Issue in code for changing an item quantity (with do/while)

    Not sure what I have wrong here, tried several attemps to change things but still does the same thing. The below code is supposed to let the user enter in an item name to search for, when found will ask for a new quantity to be stored into a variable and placed into the inventory item to update the quantity of it. It does this no problem the first time, however when i want to do it again matchedName seems to be always false and skips over the loop. Any help appreciated, thanks. Also I tried to declare matchedName as a global variable at the top of my class and set it's value to true.. but have tried without this and makes no difference.


    Code:
    System.out.println("Enter inventory NAME to update Quantity for: ");
                        
                        String testName;
                        testName = in.nextLine();
    
    
                        boolean matchedName = false;
                        testInventory.gotoBeginning(); // goes to beginning of the list - cursor points to first element in list
                        do
                        {
                                  
                            inventory = (Inventory)testInventory.getCursor(); // gets the inventory object marked by the cursor
                            if(inventory.getName().equalsIgnoreCase(testName))
                            {
                                System.out.println("Enter new item Quantity: ");
                                int newQuantity = in.nextInt();
                                inventory.setQuantity(newQuantity);
                                matchedName = true;
                                System.out.println("Item Quantity updated");
                            }
      
                        }while (testInventory.gotoNext());
                        if (!matchedName) {
                            System.out.println("Inventory Name not found - Quantity not updated!");
                            
                        }
                        break;

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Issue in code for changing an item quantity (with do/while)

    Not being able to see the entire code doesn't help much and I'm not asking for the entire source either.

    I have a few suggestions though.

    Why not use HashMap? If you need to iterate over a sorted list. TreeMap I believe.

    Keys can be the Inventory Name and the value will be the Inventory Object.
    You don't have to loop through your testInventory to get the Inventory Object.

    Lesser code and Lesser window for error.

Posting Permissions

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



Click Here to Expand Forum to Full Width