Java无法找到符号语法错误

时间:2015-05-24 05:33:23

标签: java

我是java的新手,我已经编写了下面的代码。出于某种原因,我收到了Cannot find symbol symbol: Method size() location: class Object错误消息。我读过这是一个常见的语法错误,但我无法弄清楚我哪里出错了。请指教!

import java.util.Scanner;

    public class BillboardMain {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        Billboard billboard = new Billboard();

        while (true) {
            System.out.println();
            System.out.println();
            System.out.println("******** Billboard Menu********");
            System.out.println();
            System.out.println("Please select a billboard message from the menu ");

            for (int i = 0; i < billboard.getMessages.size(); i++) {
                System.out.println((i + 1) + ": " + billboard.getMessages().get(i));
            }
            System.out.println(billboard.getMessages().size()+1 + ": Add new message.");
            System.out.println((billboard.getMessages().size() + 2) + ": Show  current text.");
            System.out.println((billboard.getMessages().size() + 3) + ": Exit.");
            System.out.println();
            System.out.print("Choice: ");
            int code = console.nextInt();

            if (code == billboard.getMessages().size()+1) {
                System.out.print("Enter new text here: ");
                String newText = console.next();
                billboard.addNewText(newText);
                System.out.println("The new text has been set to billboard");
            } else if (code == billboard.getMessages().size() + 2) {
                System.out.println("Current text is: " + billboard.getText());

            } else if (code == billboard.getMessages().size() + 3) {
                System.exit(0);
            } else {
                billboard.setText(code);
                System.out.println("The text has been set to billboard.");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

更改

for (int i = 0; i < billboard.getMessages.size(); i++) {}

for (int i = 0; i < billboard.getMessages().size(); i++) {}

正如您所做的那样:

System.out.println(billboard.getMessages().size()+1 + ": Add new message.");   

修改

  • 如果您的messages变量为String或相关对象类型,请使用billboard.getMessages().length()

  • 如果您的messages变量为List<String>或任何其他收集对象类型,请使用 billboard.getMessages().size()

  • 如果messages变量为Array Type E.g. int[],String[],etc,请使用 billboard.getMessages().length