Results 1 to 3 of 3
  1. #1
    Kerrai is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default How to move elements between Arraylists

    Hi

    So i got this problem, i need to filter out bad results into an another arraylist.
    I need to remove the entry from the first list (bookGood) and add it too the second (bookBad)

    but already when i try to remove elements from bookGood i get java.lang.IndexOutOfBoundsException

    anyone got any tips on how you could manage this?

    Another thought was to create two arraylists of the same values and then remove a value if it was faulty. Only to keep the correct entries.
    And then in the other arraylist to remove those that are correct and thus keep all the faulty ones. But even how i try i get the java.lang.IndexOutOfBoundsException

    Any help would be great
    /Martin

    Java Code:
    import java.awt.List;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class BookFix {
    	
    	
        public void Book() {
     
        	ArrayList<String> bookGood = new ArrayList<String>(); //Create an Arraylist for ok books.
        	ArrayList<String> bookBad = new ArrayList<String>(); //Create an Arraylist for ok books.
              String tmp = "";
              
              File fInput = new File("C:/users/martin/desktop/Books.txt"); //Read in the file
              Scanner scInput = null;
              try {                                                                     //Try to find the file
                 scInput = new Scanner(fInput);
             } catch (FileNotFoundException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
              while (scInput.hasNextLine()){                                            //If the file was found, read in each line to the ArrayList.
                 tmp = scInput.nextLine();
                 bookGood.add(tmp);
                 bookBad.add(tmp);
              }
              scInput.close();
         
              for (int i = 0;i < bookGood.size(); i++) {     
            	  System.out.println("\n" +
                          "Book " + (i+1) + "\n" + bookGood.get(i) + "\n");
              
                  String[] result = ((String) bookGood.get(i)).split("#");
                  for (int x=0; x<result.length; x++)
                  System.out.println(result[x]);
             
                  String isbn = (result.length > 0) ? result[0] : "";
                  String copyNumber = (result.length > 1) ? result[1] : "";
                  String title = (result.length > 2) ? result[2] : "";
                  String author = (result.length > 3) ? result[3] : "";
                  String publisher = (result.length > 4) ? result[4] : "";
                  String year = (result.length > 5) ? result[5] : "";
                  String statistics = (result.length > 6) ? result[6] : "";
                  String borrowDate = (result.length > 7) ? result[7] : "";
                  String returnDate = (result.length > 8) ? result[8] : "";
                  String libraryCardNumber = (result.length > 9) ? result[9] : "";
    
                  if ( !BookCheck.validateIsnb( isbn ) )
                	  System.out.println( "Invalid copyNumber" );
    
                  if ( !BookCheck.validateCopyNumber( copyNumber ) )
                     System.out.println( "Invalid copyNumber" );
    
                  
                  if ( !BookCheck.validateTitle( title ) )
                    System.out.println( "Invalid copyNumber" );
    
                    if ( !BookCheck.validateAuthor( author ) )
                       System.out.println( "Invalid author" );
    
                  if ( !BookCheck.validatePublisher( publisher ) )
                     System.out.println( "Invalid publisher" );
    
                  if ( !BookCheck.validateYear( year ) )
                     System.out.println( "Invalid year" );
    
                  if ( !BookCheck.validateStatistics( statistics ) )
                     System.out.println( "Invalid statistics" );
    
                  
                  if ( !BookCheck.validateBorrowDate( borrowDate ) )
                    if (borrowDate.length()==0)
                       System.out.println("");
                  else
                      System.out.println( "Invalid borrowDate" );
    
                 
                  if ( !BookCheck.validateReturnDate( returnDate ) )
               if (returnDate.length()==0)
                         System.out.println("");
                     else
                      System.out.println( "Invalid returnDate" );
    
                
                  if ( !BookCheck.validateLibraryCardNumber( libraryCardNumber ) )
                     if (libraryCardNumber.length()==0)
                         System.out.println("");
                     else
                     System.out.println( "Invalid libraryCardNumber" );
    
                  
            }
              for (int j = 0;j < bookBad.size(); j++)      {
                	  System.out.println("\n" +
                              "Book2 " + (j+1) + "\n" + bookBad.get(j) + "\n");
           }
    
    
                 
          }
        }
    Java Code:
    import java.text.*;
     
     
    public class BookCheck 
     
    {
           // validate isbn
       
        public static boolean validateIsnb( String isbn )
           {
             return isbn.matches( "\\d+" );
           } // end method validateIsnb
       
           // validate copyNumber
       
        public static boolean validateCopyNumber( String copyNumber )
           {
           return copyNumber.matches( "\\d+" );
           } // end method validateCopyNumber
       
           // validate title
       
        public static boolean validateTitle( String title )
           {
            return (title != null && title.length() > 0);
           } // end method validateTitle
       
           // validate author
       
        public static boolean validateAuthor( String author )
           {
            return (author != null && author.length() > 0);
           } // end method validateAuthor
       
           // validate publisher
       
        public static boolean validatePublisher( String publisher )
           {
            return (publisher != null && publisher.length() > 0);
           } // end method validatePublisher
       
           // validate year
       
        public static boolean validateYear( String year )
         {
              return year.matches( "\\d{4}" );
                } // end method validateYear
          
           // validate statistics
       
        public static boolean validateStatistics( String statistics )
           {
                return statistics.matches( "\\d+" );
                  } // end method validateStatistics
          
           // validate borrowDate
       
        public static boolean validateBorrowDate( String borrowDate )
           {
             SimpleDateFormat validDate = new SimpleDateFormat("yyMMdd");
              
               if (borrowDate.trim().length() != validDate.toPattern().length())
                   return false;
              
               try {
                      //parse the inDate parameter
                   validDate.parse(borrowDate.trim());
                    }
                    catch (ParseException pe) {
                      return false;
                    }
                    return true;
                  } // end method validateBorrowDate
         
           // validate returnDate
       
        public static boolean validateReturnDate( String returnDate )
           {
           SimpleDateFormat validDate = new SimpleDateFormat("yyMMdd");
           
               if (returnDate.trim().length() != validDate.toPattern().length())
                      return false;
              
               try {
                      //parse the inDate parameter
                   validDate.parse(returnDate.trim());
                    }
                    catch (ParseException pe) {
                      return false;
                    }
                    return true;
                  } // end method validateReturnDate
           // validate libraryCardNumber
       
        public static boolean validateLibraryCardNumber( String libraryCardNumber )
           {
                return libraryCardNumber.matches( "\\d+" );
                  } // end method validateLibraryCardNumber
       } // end class BookCheck
    Java Code:
    import java.io.*;
    import java.util.*;
      public class Boken {
            public static void main(String[] args) {
             
              BookFix newGame = new BookFix();
                newGame.Book();
             
              }
          }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    That constructor is ridiculously long.
    Break it down a bit.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    OK, now I have my lunch...

    I can't see where you are removing anything from bookGood.
    Why are you adding all the rows read in from the file to both bookGood and bookBad?
    Surely you just want to add them to bookGood?

Similar Threads

  1. ArrayLists for BlueJ
    By heyit'skaye in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 05:15 AM
  2. how to compare the elements of the two arraylists al1,al2
    By raj reddy in forum Web Frameworks
    Replies: 33
    Last Post: 11-25-2009, 06:48 PM
  3. how to compare the elements of these two arraylists
    By raj reddy in forum Web Frameworks
    Replies: 1
    Last Post: 03-25-2009, 11:55 PM
  4. ArrayList of ArrayLists
    By coolnfunky_raj in forum New To Java
    Replies: 10
    Last Post: 07-03-2008, 11:07 AM
  5. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 08:38 AM

Posting Permissions

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