我的数组已经完成了以前的条目

时间:2013-12-20 17:45:02

标签: java arrays

我当前的问题包含两个数组,每次我输入一本书,书籍ID就会增加一个。这似乎工作正常。但我现在的问题是,当我放入一本新书时,它完全覆盖了最后一个条目。例如John smith BookID1可能是我的第一个条目。但随后我添加了Penny Jones BookID2,约翰史密斯消失了。

要尝试解决此问题,我在类中添加了static int location = 0,并且还更改了这是数组条目,但它仍然无法正常工作。

import java.util.Scanner;

public class library {
    static Scanner keyboard = new Scanner(System.in);
    static boolean run = true;
    static int location = 0;
    public static fiction [] fictionArray = new fiction[2];
    public static nonfiction [] nonfictionArray = new nonfiction[2];


    public static void main (String[] args){                             // main class method
        while (run){                    // this while statement allows the menu to come up again
            int answer = 0;             // answer initialized to Zero
            boolean isNumber;
            do{                              // start of validation
                System.out.println("1.  Add book");      // Menu selections
                System.out.println("2.  Display the books available for loan");
                System.out.println("3.  Display the books currently on loan");
                System.out.println("4.  Make a book loan");
                System.out.println("5.  Return book ");
                System.out.println("6   Write book details to file");
                if (keyboard.hasNextInt()){                       // I would like to set values to  =>1 <=6
                    answer = keyboard.nextInt();                  // this is more validation for the input for menu selection
                    isNumber = true;
                }   else {                                        // else if number not entered, it will prompt for the correct input
                    System.out.print(" You must enter a number from the menu to continue. \n");
                    isNumber = false;
                    keyboard.next();                                // clears keyboard

                }
            }
            while (!(isNumber));                                     // while to continue program after the do has completed
            switch (answer){                                         // switch statement - uses answer from the keyboard to select a case

                case 1:
                    addBook();                                    // adds book
                    break;
                case 2:
                    for (int i=0; i<2; i++){
                    if (fictionArray[i] != null){
                            System.out.println(fictionArray[i]);}
                    if (nonfictionArray[i] != null){
                        System.out.println(nonfictionArray[i]);}}
                    break;
                case 3:
                    for (int i=0; i<2; i++){
                        if(fictionArray[i].getTitle() != "Onloan"){
                            System.out.println(fictionArray[i]);}
                        if(nonfictionArray[i].getTitle() != "Onloan"){
                            System.out.println(nonfictionArray[i]);}}break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
            }
        }
    }
        static void addBook(){
            loanbook [] loanArray = new loanbook[2];
            String title,author;
            int choice;
            boolean onloan = false;
            for(int x = 0; x < loanArray.length; x++){
            System.out.print("Press 1 for Fiction or 2 for Non Fiction: ");  // sub menu for fiction and non fiction
            choice = keyboard.nextInt();
            if (choice == 1){
                for(int count = 0; count < fictionArray.length; count++){
                    System.out.print("Enter title: ");
                    title= keyboard.nextLine();
                    title= keyboard.nextLine();
                    System.out.print("Enter author: ");
                    author= keyboard.nextLine();
                    fictionArray[location] = new fiction(title, author, onloan);
                    System.out.println("The book information you entered was :  " + fictionArray[count].toString()); // this will show the entry which was inout to the array
                    count++; }}
            else if (choice == 2) {
                for(int count = 0; count < nonfictionArray.length; count++){
                    System.out.print("Enter title: ");
                    title= keyboard.nextLine();
                    title= keyboard.nextLine();
                    System.out.print("Enter author: ");
                    author= keyboard.nextLine();
                    nonfictionArray[location] = new nonfiction(title, author, onloan);
                    System.out.println("The book information you entered was :  " + nonfictionArray[count].toString()); // this will show the entry which was inout to the array
                    count++;}}
            else{ int noBooks = loanArray.length;
                for (int i=0; i<noBooks; i++){
                    System.out.print(loanArray[x]);
                }}}} // addbook



} // Library end

超类

public class loanbook {
    private String title,author;
    static int bookID = 0;
    private boolean onloan;
    public loanbook(String pTitle,String pAuthor,boolean ponloan){
        title = pTitle;
        author = pAuthor;
        onloan = ponloan;
        bookID++;
    }  // Constructor
    public void setTitle(String pTitle){
        title = pTitle;
    } // setTitle
    protected String getTitle(){
        return title;
    }   // getTitle
    protected String getAuthor(){
        return author;
    }   // getAuthor
    public String toString(){
        return " Onloan = "+onloan +"\n BookID: "+ bookID+"\n" + " Title: "+ getTitle()+"\n" +" Author : "+ getAuthor()+ "\n";
    }

}  // loanbook

1 个答案:

答案 0 :(得分:1)

您需要在设置内容后增加位置。