2

Whenever I try to add a country into my ArrayList<country> I keep on getting this error: unexpected token: ( and it highlights my countries.add line. I'm not sure why this is happening.

class country  {
    private int mland, mwaters; //mtotalborders;
    private String mcenter;

    country(int earth, int aqua, String yn)  {
        mland = earth;
        mwaters = aqua;
        mcenter = yn;
    }
    public int getLand()  {
        return mland;
    }
    public int getWaters()  {
        return mwaters;
    }
    public int getTotalBorders()  {
        return mland+mwaters;
    }
    public String getCenter()  {
        return mcenter;
    }
}

country Turkey = new country(16, 7, "No");
country France = new country(22, 4, "No");
country England = new country(17, 9, "No");
country Germany = new country(26, 4, "Yes");
country Austria = new country(28, 1, "Yes");
country Italy = new country(17, 8, "Yes");
country Russia = new country(23, 3, "No");
ArrayList<country> countries = new ArrayList<country>();
countries.add(Turkey);
4
  • 1
    Class names should start with a capital and objects shouldn't. Following code style helps you understand other code and helps others read yours. here's a guide :) Commented Jun 20, 2014 at 17:58
  • Class name must start with uppercase character, so, country should be named as Country. Commented Jun 20, 2014 at 17:58
  • So do I only capitalize the first country or every time? I capitalized every country and lowercased the country names and still got the same error. Commented Jun 20, 2014 at 18:04
  • 1
    Using a String with two values, "Yes" or "No", is a pretty bad idea. Consider using a boolean or perhaps an enum instead. This is not related to the problem you're having, though. Commented Jun 20, 2014 at 18:26

2 Answers 2

6

You need to put your code into a method - you probably want to use the main method - see below.

    ......

    public String getCenter()  {
        return mcenter;
    }

    public static void main(String[] args){
        country Turkey = new country(16, 7, "No");
        country France = new country(22, 4, "No");
        country England = new country(17, 9, "No");
        country Germany = new country(26, 4, "Yes");
        country Austria = new country(28, 1, "Yes");
        country Italy = new country(17, 8, "Yes");
        country Russia = new country(23, 3, "No");
        ArrayList<country> countries = new ArrayList<country>();
        countries.add(Turkey);
    }
}

Note: The proper convention is to capitalize Class names, and have variable names lowercase.

This will require you to change your Class name in your code - see below.

class Country  {
    private int mland, mwaters; //mtotalborders;
    private String mcenter;

    Country(int earth, int aqua, String yn)  {
        ......

Also anytime you reference the Class name. For example.

Country turkey = new Country(16, 7, "No");
Sign up to request clarification or add additional context in comments.

3 Comments

In Processing, these should actually probably be in a setup method, not a main function.
As addition: you can also put the countries.add in an initializer block, if you want them to be added at start.
@JakeKing Given the nature of the question I'm operating under the assumption that this is an intro hw exercise so giving the basics here.
1

Lufval, you're trying to write code outside of a class definition. Global variable declarations are OK, executable statements are not.

1 Comment

Java doesn't have global variables outside of classes. I think you mean trying to write code outside of a 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.