0

I am new to java and I a have written the code below. For some reason I'm getting Cannot find symbol symbol: Method size() location: class Object error message. I have read it is a common syntax error, but I cannot figure out where I went wrong. Please advise!

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
  • 2
    change getMessages to getMessages(); Commented May 24, 2015 at 5:35

1 Answer 1

2

Change

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

to

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

As you have done in:

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

Edit

  • If your messages variable is String or related Object type use billboard.getMessages().length()

  • If your messages variable is List<String> or any other Collection Object type use billboard.getMessages().size()

  • If your messages variable is Array Type E.g. int[],String[],etc use billboard.getMessages().length

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, I made the change, but the error still exists. The size() seems to be the issue as it is underlined in red and the error appears only in the lines where size() is written.
what is variable type for messages?
I don't understand, because my variable is List<String>, so it should be working, but it is not.
Cannot find symbol symbol: method size() location Class object
can you show the declaration of your variable and check whether it has proper getter/setter method?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.