繁体   English   中英

尝试使用班级中的一种方法打印出ArrayList,我在做什么错?

[英]Trying to print out my ArrayList using one of my methods in my class what am i doing wrong?

这是我的程序。 我想做的就是在末尾打印ArrayList,但不会,我在做错什么,我试图使用mls.ToString(),但是那不起作用。 请告诉我我做错了。 PS这是家庭作业,感谢您的帮助。

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;
}

}

mls不是一Book 这是一个ArrayList<Book> 您需要在调用ToString()之前从列表中获取书籍。 类似于以下内容:

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

您正在尝试访问书籍List "mls"的方法ToString ,该方法没有这样的方法,您的变量不是书籍而是书籍列表。

您必须将"mls.ToString()"替换为"usersBook.ToString()" ,才能访问您的方法。

PS:您应该使用小写的toString重命名您的方法,在Java中,所有对象都隐式继承该方法,您可以/应该覆盖它。

问题符合:

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

ArrayList没有ToString()方法。 ArrayList具有toString()方法。

解决方法是:

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

要么

System.out.println(mls);

您无需使用toString()方法,它将被自动调用。

第二个问题是Book类中的toString()方法应类似于:

@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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM