method rather than the class block. Logically it probably makes sense to place all statements in the code block but the non- declarative statements. Most likely you have placed your code outside of a method or class. Try the following: import java. ArrayList; import java. List; public class Test { public Test( ) { List< String> list = new ArrayList< String> ( ) ; list. add( " Monkey" ) ; }. You can' t have code just floating around in the class. You might want to put it in a staic initialization block: public static ArrayList< Float> vat_ values = new ArrayList < Float> ( ) ; static { vat_ values. Syntax error on token " " pizza" ", delete this token; Syntax error on token( s), misplaced construct( s). java string arraylist. List Generics List interface. List< String> food_ names = new ArrayList< String> ( ) ; food_ names. add( " pizza" ) ;. This shouldn' t give any error.
The compiler is trying to parse food_ names. add( " pizza " ) ; as a declaration, and is getting thoroughly confused. share| improve this. public class Books { private ArrayList< String> booksDB; / / this is a field of the Books class / / when we create a new Books instance, we will initialize the booksDB field public Books( ) { booksDB = new ArrayList< String> ( ) ;. Place this code inside your onCreate( ) method: final ListView myLV = ( ListView) findViewById( R. myLV) ; final EditText myET = ( EditText) findViewById( R. myET) ; final ArrayList< String> todoList = new ArrayList< String> ( ) ; final. Syntax error on token( s), misplaced construct( s). public class Inventory { private List inventory = new ArrayList( ) ; public Inventory( ) { String item1 = " Sword" ; String item2 = " Potion" ; String item3. No compiler error there because declarations are allowed in class body. I could have put it in a method, but I chose the constructor because its usual role is to initialize class members. You have class OS {. and also public class OS extends Frame {. The first one should be removed.
Other than that, the object ' slot' was not declared and initialized. After removing class OS {. and two lines of slot. The reason why your code does not work is that you tried to write code in the class body. public class Inventory { private List inventory = new ArrayList( ) ; public Inventory( ) { String item1 = " Sword" ; String item2 = " Potion" ; String item3 = " Shield" ;.