我可以使用一些帮助来解决这个问题:ArrayIndexOutOfBoundsException

时间:2015-03-18 15:22:08

标签: java indexoutofboundsexception

  package Books;
import java.util.*;
class Book{
    private int isbn;
    private String title;
    private static int count=0;

    public Book(){
        count ++;
        }
    public void readBook(){

    int isbn=0;
    Scanner input = new Scanner(System.in);
    String temp;

        Book book = new Book(); 
        System.out.print("ISBN: ");
        temp=input.nextLine();
        book.setIsbn(Integer.parseInt(temp));

        System.out.print("title: ");
        String title = input.nextLine();
        book.setTitle(title);
    }
    public int getCount(){
        return count;
    }
    public void incrCount(){
        this.count++;
    }
    public int getIsbn() {
        return isbn;
    }
    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }   
}

主要的一个:

    package Books;

import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Ionut
 */
public class Books {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, NumberFormatException{

        Scanner input = new Scanner(System.in);
        Book book = new Book();
        int choice;
        Book[] books = new Book[10];
        int currentIndex;
        String temp;

        for (int i = 0; i < books.length; i++) {
            books[i] = new Book();

        }

        while(true){
        do{
            System.out.println("Library System");
            System.out.println("       1. add book");
            System.out.println("       2. display books");
            System.out.println("       3. save to file");
            System.out.println("       4. load from file");
            System.out.println("       0. exit");
            System.out.print("choice: ");
            temp = input.nextLine();
            choice = Integer.parseInt(temp);
        }while ((choice < 0) || (choice > 4));

        switch (choice){

            case 1: //read isbn, title
                    currentIndex=book.getCount();
                    book.readBook();
                    //add current book to the array

                    books[currentIndex+1] = book;
                    book.incrCount();
                    break;
            case 2: //display array of books
                    currentIndex=book.getCount();
                    System.out.println("ISBN         Title   ");
                    for(int i=0; i< currentIndex; i++){
                    System.out.println(books[i].getIsbn()+"  "+books[i].getTitle()+"  ");
                    }
                    break;
            case 3: break;
            case 4: break;
            case 0: return ;

        }
        }//end while true
       }
}

在主要内容中,我介绍了`

for (int i = 0; i < books.length; i++) {
            books[i] = new Book();
        }

` 现在我不再收到NullPointerException错误,但是我得到了ArrayIndexOutOfBoundsException。

run:
Library System
       1. add book
       2. display books
       3. save to file
       4. load from file
       0. exit
choice: 1
ISBN: 1
title: 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
    at Books.Books.main(Books.java:49)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)

任何帮助都是很好的。 我不明白为什么我得到这个错误,我刚刚初始化数组为什么它会超出界限?

1 个答案:

答案 0 :(得分:0)

我认为问题出在开关1中,因为++导致它出现了界限。你从未指定它不应该添加更多,如果它在数组的最后一本书。

啊,是的,你可以像这里所说的那样使用ArrayList。我相信你可以做arraylist.add(书);或者其他什么,arraylists创建没有最大不是吗?