Results 1 to 2 of 2
- 06-07-2012, 01:48 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
I do not know what i am doing wrong with my library card program
i had a Java Programming class last semester and passed with a B because i never finished my last project. Already done, i would like to finish it for my own sakes, this is the code i typed up and i kept receiving
so if anyone could help me, bellow i have my program and at the very bottom i have the two read in files. this is just to see if there are any advance programmers who know what they are doing.Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30 at Library.main(Library.java:23)
books.txtJava Code:import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class Library { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); int j=0; // declare an array of 20 cards Card[] Cards = new Card [ 30 ]; // declare an array of 20 books Book [] Books = new Book [ 20 ]; // declare a file pointer and open the cards file File file = new File ("cards.txt"); Scanner fileinput = new Scanner(file); // read the next line from the file String line= fileinput.nextLine(); // tokenize the line to get the data for class variables String tokens [] = line.split(","); while (fileinput.hasNext()){ Cards[j] = new Card( tokens[0], tokens[1], Integer.parseInt(tokens[2]), 0 ); j++; } // declare a file pointer and open the books file File file1 = new File ("books.txt"); Scanner fileinput1 = new Scanner(file1); // read the next line from the file String line1= fileinput1.nextLine(); // tokenize the line to get the data for class variables String tokens1 [] = line1.split(","); while (fileinput1.hasNext()){ Books[j] = new Book( tokens1[0], tokens1[1], Integer.parseInt(tokens1[2]), false , 0 ); j++; } // display main menu ShowMenu(); int command = input.nextInt(); while(command != 0) { switch ( command ) { case 1: for(int i = 0; i < Cards.length; i++){ System.out.println(Cards[i]); } break; case 2: for(int i = 0; i < Books.length; i++){ System.out.println(Books[i]); } break; case 3: System.out.println( "Please Enter your Student ID:" ); int OutCardID = input.nextInt(); for(int i = 0; i < Cards.length; i++){ } System.out.println( "Please Enter the Book ID:" ); int OutBookID = input.nextInt(); break; case 4: System.out.println( "Please Enter your Student ID:" ); int InCardID = input.nextInt(); System.out.println( "Please Enter the Book ID:" ); int InBookID = input.nextInt(); break; case 5: System.out.println( "Please Enter Your Name: " ); String Name =input.next(); System.out.println( "Please Enter your Phone Number:" ); String Phone =input.next(); System.out.println( "Please Enter the last three numbers of Student ID:" ); int CardID=input.nextInt(); Card NewCard = CreateCard(Name,Phone,CardID); break; case 6: System.out.println( "\nExiting the menu..." ); command = 0; return; default: System.out.println( "\nYou did not enter a valid selection. Try again." ); break; } ShowMenu(); command = input.nextInt(); } } public static void ShowMenu() { System.out.println("\nMain Menu:"); System.out.println("1 - Show all library cards "); System.out.println("2 - Show all books "); System.out.println("3 - Check out a book "); System.out.println("4 - Check in a book "); System.out.println("5 - Create a new library card "); System.out.println("6 - Exit the system\n "); System.out.println("Enter a choice: "); return; } public static Card CreateCard(String Name, String Phone, int CardID) throws IOException { File file = new File ("cards.txt"); Scanner fileinput = new Scanner(file); FileWriter outFile = new FileWriter("cards.txt"); PrintWriter out = new PrintWriter(outFile); out.println(Name+","+Phone+","+CardID+",0"); return null; } public static class Card { String Name; String Phone; int CardID; int BookID; public Card(String Name, String Phone, int CardID, int BookID) { this.Name= Name; this.Phone=Phone; this.CardID=CardID; this.BookID=BookID; } } public static class Book { String Booktitle; String Author; int BookID; boolean BookCheckout; int WhoHasBook; public Book(String Booktitle, String Author, int BookID, boolean BookCheckout, int WhoHasBook) { this.Booktitle=Booktitle; this.Author=Author; this.BookID=BookID; this.BookCheckout=BookCheckout; this.WhoHasBook=WhoHasBook; } } }
War and Peace, Tolstoy,456,false,0
Programming in C, Deitel,123,false,0
The Boat Who Wouldn't Float,Mowat,987,false,0
The Secret, Byrne,876,false,0
The power of Intention,Dyer,765,false,0
cards.txt
Mike Sullivan,443-207-1111,123,0
Mary Surber,401-682-2222,234,0
Ann Kidd,302-456-9854,345,0
Todd Chaplin,301-234-0000,456,0
to anyone who knows, thank you
- 06-07-2012, 02:49 AM #2
Re: I do not know what i am doing wrong with my library card program
The index to an array at line 23 is past the end of the array. Look at that line, find the array and index and determine why the index goes past the end of the array. Remember that array indexes start at 0 and go to the array length -1. The max index for an array with 30 elements is 29.xception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
at Library.main(Library.java:23)If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Bi-directional card reading Reads up to two tracks of card data DES/TDES Encryption
By mageshge21 in forum Advanced JavaReplies: 4Last Post: 06-15-2012, 06:10 AM -
java card program
By firdose in forum New To JavaReplies: 5Last Post: 02-14-2012, 01:51 PM -
Question Card Layout, Card Management
By lrichil in forum AWT / SwingReplies: 1Last Post: 04-22-2010, 10:11 AM -
importing a library into my program
By sniffer139 in forum NetBeansReplies: 1Last Post: 03-22-2010, 01:29 PM -
Card program , need help thanks.
By carlos123 in forum New To JavaReplies: 2Last Post: 12-31-2007, 07:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks