Results 1 to 10 of 10
  1. #1
    dloexcitement is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Thumbs down Simple question on why getting this error

    Hey i have an Assignment due soon, and i am trying to write a program that helps me read a file on my computer, and then uploads each section into an array. I just need to know why i am getting this error. I am not sure if this is that difficult, but it sure is giving me a hard time.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Library.main(Library.java:23)

    for this code:

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class Library {
    	public static void main(String[] args) throws FileNotFoundException {
    	Scanner input = new Scanner(System.in);
    	int j=0;
    	// declare an array of 20 cards
    		Card[] Cards = new Card [ 20 ];
    	// declare an array of 20 books
    		Book [] Books = new Book [ 20 ];
    	// declare a file pointer and open the cards file 
    		Scanner fileinput = new Scanner("cards.txt");
    		while (fileinput.hasNext()){
    	 		// read the next line from the file
    	 	 	String line= fileinput.nextLine();
    	 	// tokenize the line to get the data for class variables
    	 	 	for (j=0;j<Cards.length; j++){
    	 	 	String tokens [] = line.split(",");
    	 		Cards[j] = new Card( tokens[0], tokens[1], Integer.parseInt(tokens[2]), 0 );
    		 	j++;
    	 	 	}
    		}
    				
    
    	// declare a file pointer and open the books file 
    	 	Scanner fileinput1 = new Scanner("books.txt");
    	 	while (fileinput1.hasNext()){
    	 // 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(","); 
    	 	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]);
    		}
    		case 2:
    		for(int i = 0; i < Books.length; i++){
    		System.out.println(Books[i]);
    		}
    		case 3:
    
    
    		case 4:
    
    
    		case 5:
    		
    		break;
    		case 6:
    		System.out.println( "\nExiting the menu..." );
    		break;
    		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, int BookID)
    {
    	
    	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;
    	}
    
    }
    }
    
    
    these are the text documents:
    
    books.txt
    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
    thank you, -D
    Last edited by dloexcitement; 04-28-2012 at 05:14 AM.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Simple question on why getting this error

    Use code brackets pleeeeease...

  3. #3
    dloexcitement is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Simple question on why getting this error

    what are code brackets?

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Simple question on why getting this error

    Code brackets:
    Java Code:
    public static void ShowMenu()
    {
    System.out.println("\nMain Menu:");
    }
    enclose your code with the forum tags [code ] and [/code ]

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Simple question on why getting this error

    You should check if tokens contains at least 3 items, else you will get this error because you access token[2]...

  6. #6
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Simple question on why getting this error

    Java Code:
    for (j=0;j<Cards.length; j++){
    String tokens [] = line.split(",");
    Cards[j] = new Card( tokens[0], tokens[1], Integer.parseInt(tokens[2]), 0 );
    j++;}
    I'm pretty sure this'll be an issue at some point. Don't, unless you really know what you're doing, mess with the incrementing variable inside a loop.

    In addition, lines 23 is somewhere in the above, presumably the bit where you access Cards[]

    My bet? You read in a line with nothing on it. Thus, tokens[0] = '\n' and tokens[1] doesn't exist.

  7. #7
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Simple question on why getting this error

    Quote Originally Posted by Diargg View Post
    My bet? You read in a line with nothing on it. Thus, tokens[0] = '\n' and tokens[1] doesn't exist.
    Just what I said, right? He needs to check for the length of tokens, even if the line is not empty... ;)

  8. #8
    dloexcitement is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Simple question on why getting this error

    yea its saying its line 22, but i dont know how to put the tokens into the array so that i can print them out later, how would you suggest? with a while loop?

  9. #9
    dloexcitement is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Simple question on why getting this error

    how would you check the length of your token mind me asking

  10. #10
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Simple question on why getting this error

    You get the length of "tokens" is tokens.length and when you read the line and have split it you should check it. If it is not larger or equal the number of elements you need or expect you just skip the process and go on with your while loop.

    As you already suggested the best would be to put the tokens into an array... suggestion:
    1. Create a new class (let us call it "Match") that contains the "home" and "visitor" string as well as the scores as variables
    2. Make for the start the four variables inside this class "public"
    3. Create an array list (use ArrayList<Match>) to store your newly created objects by using list.add(newmatch)
    4. Implement a toString() method in your new Match class that puts all four variables into a String and returns this one
    5. Make a loop in the main class printing out your ArrayList using the toString() method

Similar Threads

  1. Simple question...
    By Onyx in forum New To Java
    Replies: 10
    Last Post: 08-10-2011, 11:44 PM
  2. Simple question
    By Qsc in forum New To Java
    Replies: 6
    Last Post: 03-07-2011, 12:24 AM
  3. 30 line simple gui, simple error, need help
    By cc11rocks in forum New To Java
    Replies: 6
    Last Post: 02-05-2011, 09:33 PM
  4. very simple Question
    By arsenal4ever_11 in forum NetBeans
    Replies: 2
    Last Post: 05-27-2010, 09:51 PM
  5. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 06:32 PM

Posting Permissions

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