2016-05-14 10 views
0

私のプログラムです。私がしようとしているのは、ArrayListを最後に表示することです。何も間違っています.mls.ToString()を使用しようとしていますが、動作していません。私が間違ったことを教えてください。 PSこれは宿題であり、助けてくれてありがとう。ここで私のクラスのメソッドの1つを使ってArrayListを印刷しようとしていますが、何が間違っていますか?

import java.util.ArrayList; 
import java.util.Scanner; 

public class Main { 

public static void main(String[] args) { 
    ArrayList<Book> mls = new ArrayList<Book>(); 
    String userAuthor; 
    String userTitle; 
    int userYearPub; 
    int userPageCount; 

    String answer; 
    int numberAnswer; 
    int choice; 


    do { 
     Scanner in = new Scanner(System.in); 

     System.out.println("Please enter the author of the book."); 
     userAuthor = in.next(); 
     System.out.println(); 

     System.out.println("Please enter the title of the book."); 
     userTitle = in.next(); 
     System.out.println(); 

     System.out.println("Please enter the year the book was published."); 
     userYearPub = in.nextInt(); 
     System.out.println(); 

     System.out.println("Please enter the number of pages of the book."); 
     userPageCount = in.nextInt(); 
     System.out.println(); 

     Book usersBook = new Book(userAuthor, userTitle, userYearPub, userPageCount); 

     System.out.println(usersBook.ToString()); 
     System.out.println(); 

     do { 
      System.out.println("If you want to change anything press one of the following options: "); 
      System.out.println('1' + " Author."); 
      System.out.println('2' + " Title."); 
      System.out.println('3' + " Year it was published."); 
      System.out.println('4' + " Number of pages"); 
      System.out.println('5' + " Quit"); 
      System.out.println(); 
      choice = in.nextInt(); 

      if (choice == 1) { 
       System.out.println("Please enter the new author:"); 
       answer = in.next(); 
       usersBook.setAuthor(answer); 
      } 

      if (choice == 2) { 
       System.out.println("Please enter the new title:"); 
       answer = in.next(); 
       usersBook.setTitle(answer); 
      } 

      if (choice == 3) { 
       System.out.println("Please enter the new year:"); 
       numberAnswer = in.nextInt(); 
       usersBook.setYearPub(numberAnswer); 
      } 

      if (choice == 4) { 
       System.out.println("Please enter the new pages count:"); 
       numberAnswer = in.nextInt(); 
       usersBook.setPages(numberAnswer); 
      } 

      if (choice == 5) { 
       break; 
      } 

      System.out.println(); 
      System.out.println("Is this correct?"); 
      System.out.println('1' + " yes"); 
      System.out.println('2' + " no"); 
      choice = in.nextInt(); 

      if (choice == 1) break; 

     }while (choice == 2); 

     mls.add(usersBook); 

     System.out.println(usersBook.ToString()); 

     System.out.println(); 
     System.out.println("Do you want to input another book?"); 
     System.out.println("If so press 1, if not press 2."); 
     choice = in.nextInt(); 

     System.out.println(mls.ToString()); 

    } while (choice == 1); 

} 

}

Bookクラスです。

public class Book { 
private String author; 
private String title; 
private int yearPub; 
private int pages; 

public Book(String Author, String Title, int YearPub, int Pages) 
{ 
    this.author = Author; 
    this.title = Title; 
    this.yearPub = YearPub; 
    this.pages = Pages; 
} 
public String ToString() 
{ 
    return "The Author of this book is " + author + 
      "\nThe title of this book is " + title + 
      "\nThe year published is " + yearPub + 
      "\nThe number of pages is " + pages; 
} 

public void setAuthor(String newSring) 
{ 
    author = newSring; 
} 
public void setTitle(String newString) 
{ 
    title = newString; 
} 
public void setYearPub(int newValue) 
{ 
    yearPub = newValue; 
} 
public void setPages(int newValue) 
{ 
    pages = newValue; 
} 

}

+1

私はそれが宿題ですので、オフトピックとして、この質問を閉じるために投票しています:

そして、第二の問題は、BookクラスのあなたのtoString()メソッドは次のようになりべきであるということです。 – Johan

+0

アドバイス:if文ではなく 'switch'を使用してください。特に、小さな範囲の数字を入力する必要がある場合には、 また、常に 'this'はコンストラクタ/ get/setメソッドで' this.autor = autor'を許可するという目的のために、小文字で変数を始めるべきです。 – another

+0

@Mikは長い時間前に解決したと思いますか? =) – another

答えて

0

mlsBookありません。それはArrayList<Book>です。 ToString()に電話する前に、リストから書籍を取り出す必要があります。以下のような何か:

for(int i = 0; i < mls.size(); i++) { 
    System.out.println(mls.get(i).ToString()); 
} 
0

あなたがいないような方法を持っている図書"mls"Listの方法ToStringにアクセスするtriyingされている、あなたの変数は、本は本のリストがされていません。

お客様の方法にアクセスするには、"mls.ToString()""usersBook.ToString()"に置き換える必要があります。

PS:メソッド名を小文字のtoStringに変更する必要があります。Javaでは、すべてのオブジェクトがそのメソッドを暗黙的に継承しています。

System.out.println(mls.ToString()); 

のArrayListのToString()メソッドを持っていない:

+1

私が得意でないのは、なぜ彼が 'mls.ToString()'を呼び出すときに例外が発生しないかということです。もちろん、ArrayListクラスにはその名前のメソッドがありません。おそらく、彼はそうしているだけで、私たちに語ったことはありません。ああ... – Gendarme

+0

実際に彼は例外を取得します。 – another

0

問題を含む行です。 ArrayListにはtoString()メソッドがあります。

ソリューションは、次のとおりです。

System.out.println(mls.toString()); 

または

System.out.println(mls); 

あなたは、toString()メソッドを使用しmusn't、それが自動的に呼び出されます。

@Override 
public String toString() { 
    return "The Author of this book is " + author + 
      "\nThe title of this book is " + title + 
      "\nThe year published is " + yearPub + 
      "\nThe number of pages is " + pages; 
} 
関連する問題