2

I am trying to add a string to an ArrayList in Java, but I can't seem to get it to work.

Currently, I have the following code:

List food_names = new ArrayList();

food_names.add("pizza");

Why do I get these errors:

  • Syntax error on token ""pizza"", delete this token
  • Syntax error on token(s), misplaced construct(s)
6
  • 4
    What language? ArrayList exists in at least Java and .NET. It does not look like .NET errors though. Commented Feb 20, 2011 at 8:04
  • What language are you using? Java? Commented Feb 20, 2011 at 8:05
  • did you copy and paste the code? eg I'm wondering if you're using the wrong quotes like you may get from pasting from MS Word Commented Feb 20, 2011 at 8:09
  • Nope. Typing them in manually Commented Feb 20, 2011 at 8:10
  • 1
    Is this the exact code or is there anything in between? Also, you might want to use List<String> food_names = new ArrayList<String>(); Commented Feb 20, 2011 at 8:12

6 Answers 6

6

You have to use food_names.add("pizza") in function, for example:

public void AddFood()
{
  food_names.add("pizza");
}

Hope helps

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

3 Comments

Thank you. That did it. I'm not used to such strict languages. I kinda wish the compiler gave more useful error messages.
@mellowg - learn to live with it. The compiler cannot read your mind to figure out what you were really trying to do. (The same thing applies to a non-strict language ... with the added bonus that the compiler is more likely to decide on a meaning for your code that is not what you thought you'd written.)
Your answer is correct, but please stick to the widespread Java conventions - use a lowercase character as first method name character. See docstore.mik.ua/orelly/java-ent/jnut/ch07_01.htm
5

why don't you use List Generics List interface.

 List<String> food_names = new ArrayList<String>();

food_names.add("pizza");

This shouldn't give any error.

Comments

3

I suspect that those statements are at the top level of a class. The first one is OK there, but the second one can only be inside a code block; e.g. a method or constructor body. See @tomasBull's answer for an example of where you can do that.

The compiler is trying to parse food_names.add("pizza"); as a declaration, and is getting thoroughly confused.

Comments

2

There is another solution

food_names.add(new String("pizza"));

Comments

0
Class Example 
{

//**Do not place it here.**
List food_names = new ArrayList();
food_names.add("pizza");

 public static void main(String[] args)
{

//**You should place it here**
List food_names = new ArrayList();
food_names.add("pizza");

}


}

Comments

0

I'm not sure you're initializing the ArrayList correctly. Try

ArrayList<String> food_names = new ArrayList<String>();

Also double check you've imported ArrayList. Or try removing the quotation marks around pizza. Disclaimer: I'm probably wrong but I'm laying in bed typing this from my phone, haha. Good luck.

2 Comments

Thanks for the edit, the browser on my phone was giving me issues.
Found you, Rahul Phulore . . . . :D

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.