我是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.");
}
}
}
}
答案 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