Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'(' or '[' Expected

Tags:

java

arrays

I am getting the following error when trying to compile my program.

'(' or '[' Expected.

 public AccountArrayList()
{
    // line one below is the hi-lighted code
    ArrayList accounts = new ArrayList;
    accounts.add("1");
    accounts.add("1");
    accounts.add("1");
    accounts.add("1");
    accounts.add("1");
    accounts.add("1");
    accounts.add("1");
    accounts.add("1");
    accounts.add(5,"900");
}

Thank you.

like image 587
Chris Avatar asked Nov 29 '10 10:11

Chris


People also ask

What does error class expected mean?

The class interface or enum expected error is a compile-time error in Java which arises due to curly braces. Typically, this error occurs when there is an additional curly brace at the end of the program.

What does .class expected mean?

class' expected. public class Calculator{ public Calculator(){ } public int sum(int one, int two) { int s = one + two; return int s; } } This error usually means that you are trying to declare or specify a variable type inside of return statement or inside of a method calls.

What does expected mean in Python?

The error you're getting is stating that it expected to get an expression it could evaluate. Instead it sees that you are trying to make a variable assignment in your if statement.


3 Answers

You're missing parenthesis on the constructor:

ArrayList accounts = new ArrayList();
like image 64
Yuval Adam Avatar answered Nov 14 '22 23:11

Yuval Adam


Your constructor is wrong. It has to be;

ArrayList accounts = new ArrayList();
like image 34
RoflcoptrException Avatar answered Nov 14 '22 22:11

RoflcoptrException


If you are using Java 5 and higher, you will see that ArrayList uses generics.

You can essentially to this:

ArrayList<String> accounts = new ArrayList<String>();
like image 33
Buhake Sindi Avatar answered Nov 14 '22 21:11

Buhake Sindi