首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试使用我的类中的一个方法打印出我的ArrayList,我做错了什么?

尝试使用我的类中的一个方法打印出我的ArrayList,我做错了什么?
EN

Stack Overflow用户
提问于 2016-05-15 01:05:04
回答 3查看 59关注 0票数 0

这是我的程序。我所要做的就是在最后打印ArrayList,但它不会,我做错了什么,我试图使用mls.ToString(),但这不起作用。请告诉我我做错了什么。这是家庭作业,谢谢你的帮助。

代码语言:javascript
运行
复制
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类。

代码语言:javascript
运行
复制
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;
}

}

EN

回答 3

Stack Overflow用户

发布于 2016-05-15 01:16:20

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

代码语言:javascript
运行
复制
for(int i = 0; i < mls.size(); i++) {
    System.out.println(mls.get(i).ToString());
}
票数 0
EN

Stack Overflow用户

发布于 2016-05-15 01:43:31

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

为了访问你的方法,你必须用"usersBook.ToString()"替换"mls.ToString()"

PS:你应该用小写的toString来重命名你的方法,在Java语言中,所有的对象都隐式地继承了这个方法,你可以/应该覆盖它。

票数 0
EN

Stack Overflow用户

发布于 2016-05-15 07:29:25

问题是这样的:

代码语言:javascript
运行
复制
System.out.println(mls.ToString());

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

解决方案是:

代码语言:javascript
运行
复制
System.out.println(mls.toString());

代码语言:javascript
运行
复制
System.out.println(mls);

你不能使用toString()方法,它会被自动调用。

第二个问题是Book类中的toString()方法应该如下所示:

代码语言:javascript
运行
复制
@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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37229515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档