7 Replies Latest reply on Apr 24, 2007 11:59 AM by 807606

    Changing Objects attributes from an ArrayList

    807606
      Can please somebody help me? Im trying to change the borrower number of the book found, when the search method generates only one book. The following code change the borrower to the last book object of the ArrayList. Does someone know what is wrong with this code?
      public void search()
          {
              int borrower = Integer.parseInt(borrowerTextField.getText());
              String entry = descriptionTextField.getText();
              index = 0;
              while(index < books.size()){
                  Book book = books.get(index);
                  if (book.getDescription().equals(entry) || book.getDescription().contains(entry)){    
                   System.out.println(book.getDescription()); }
                   
                  if (book.getBorrowerNumber() == 0 && book.getDescription().equals("")){        
                   System.out.println(book.getDescription()) ; }
                  
                  if (book.getBorrowerNumber() == borrower && book.getBorrowerNumber() != 0){   
                    System.out.println(book.getDescription());  }
                    
               
                  index++;
                     
                  }    
                 
          }  
          
          public void borrowBook ()
          {
             borrower = Integer.parseInt(borrowerTextField.getText());
             Book book = books.get(index-1);
             if(borrower !=0){
              book.setBorrowerNumber(borrower);}
             else{
                 System.out.println("please enter you ID");} 
             
           }
        • 1. Re: Changing Objects attributes from an ArrayList
          807606
          by calling search() method, index will reach the last index of the list every time, since the only condition to stop the loop is to reach the last index of the list

          therefore, if you want to stop the loop when the book is found, you have to use "break;" statement in your loop once the book is found, so as to break the loop, and have "index" pointing on the good index

          once you have the good index, you can change the borrower
          • 2. Re: Changing Objects attributes from an ArrayList
            807606
            The problem is that the variable index is not set properly in search(). In the loop

            while(index < books.size()){
            ......
            index++;
            }

            you keep on incrementing the index even if you have found the book. Thus the index is always equal to books.size() after the completion of loop. You could break from the loop as soon as you get the book (your if condition is true).
            I think changing void search() to int search() where search() returns the index of the book searched would be better.
            • 3. Re: Changing Objects attributes from an ArrayList
              807606
              The problem by using the break statement is that once it found one book the loop will stop. The search can generate more than one book, depending on the String input by the user. The borrowerBook method should only work when the search generate only one book.
              • 4. Re: Changing Objects attributes from an ArrayList
                807606
                try this one:
                int numberOfBookFound; int bookIndex;
                public void search()
                    {
                        int borrower = Integer.parseInt(borrowerTextField.getText());
                        String entry = descriptionTextField.getText();
                        index = 0; numberOfBookFound = 0; bookIndex = -1;
                        while(index < books.size()){
                            Book book = books.get(index);
                            if (book.getDescription().equals(entry) || book.getDescription().contains(entry)){    
                             System.out.println(book.getDescription()); numberOfBookFound++; bookIndex = index;}
                             
                            if (book.getBorrowerNumber() == 0 && book.getDescription().equals("")){        
                             System.out.println(book.getDescription()) ; numberOfBookFound++; bookIndex = index; }
                            
                            if (book.getBorrowerNumber() == borrower && book.getBorrowerNumber() != 0){   
                              System.out.println(book.getDescription()); numberOfBookFound++; bookIndex = index; }
                              
                         
                            index++;
                               
                            }    
                           
                    }  
                    
                    public void borrowBook ()
                    {
                       if (numberOfBookFound == 1) {
                          borrower = Integer.parseInt(borrowerTextField.getText());
                          Book book = books.get(bookIndex-1);
                          if(borrower !=0){
                             book.setBorrowerNumber(borrower);}
                          else{
                             System.out.println("please enter you ID");} 
                       }
                     }
                • 5. Re: Changing Objects attributes from an ArrayList
                  807606
                  Thanks sooo much, that works.. :D
                  • 6. Re: Changing Objects attributes from an ArrayList
                    807606
                    Only one more question... don't really undestand.. why is the bookIndex = -1?
                    • 7. Re: Changing Objects attributes from an ArrayList
                      807606
                      i (and some others programers, i think) use -1 as a synonym for "not found", in my codes

                      for example, if you try to use "indexOf("toto")" on string "tatatutu", it will return -1, as toto does not appear in tatatutu

                      before browsing the list, the book is marked as "not found", so i initialize it with -1

                      the second reason why i use -1 and not 0 or a positive number is that if i miss an instruction like the "if ( ** == 1)" in your example, it will throw me an indexoutofboundsexception, and i clearly know that there's something going wrong in my code, whereas if i put "1" as a default value, code will work but it will acts odd, and it will prolly be a bit more difficult for me to see where does the bug come from :)

                      it can actually be any number in this case ; i just used it 'coz of these 2 reasons