Results 1 to 3 of 3
- 11-04-2010, 12:04 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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 BookCheckJava Code:import java.io.*; import java.util.*; public class Boken { public static void main(String[] args) { BookFix newGame = new BookFix(); newGame.Book(); } }
- 11-04-2010, 12:11 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
That constructor is ridiculously long.
Break it down a bit.
- 11-04-2010, 12:44 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
ArrayLists for BlueJ
By heyit'skaye in forum New To JavaReplies: 1Last Post: 09-01-2010, 04:15 AM -
how to compare the elements of the two arraylists al1,al2
By raj reddy in forum Web FrameworksReplies: 33Last Post: 11-25-2009, 05:48 PM -
how to compare the elements of these two arraylists
By raj reddy in forum Web FrameworksReplies: 1Last Post: 03-25-2009, 10:55 PM -
ArrayList of ArrayLists
By coolnfunky_raj in forum New To JavaReplies: 10Last Post: 07-03-2008, 10:07 AM -
arraylists problem
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-12-2008, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks