0

I can't figure out why I can't add items to the ArrayList. I have tried a couple different ways of adding items and they don't work.

class Problem {
public ArrayList<String> problems = new ArrayList<String>();
public ArrayList<String> answers = new ArrayList<String>();
private String question1 = "What is 2+2?";
private String question2 = "What is the square root of 25";
private String question3 = "What is the next number in the sequence? {2, 4, 6}";
private String question4 = "What is 8*8?";
String[] temp1 = {question1, question2, question3, question4};
for (String s : temp1)
 problems.add(s);
}

I have also tried

problems.add(question1);
problems.add(question2);
problems.add(question3);
problems.add(question4);

This does not work either.

Compiler says that identifier is expected.

Borealid
99.2k9 gold badges111 silver badges123 bronze badges
asked Jan 29, 2012 at 4:00
1
  • Adding the items needs to be in a method or in an initializer. Why do you put them in an array then add to the list? Why not just add them to the list? Commented Jan 29, 2012 at 4:05

3 Answers 3

7

Try adding import java.util.ArrayList; to the top of your file.

You also need a main method to run, with a signature like this:

public static void main(String[] args)

Also, you should make the string constants final (and/or static) if you don't plan on changing them.

Finally, temp1 is the default ("friend") visibility.

I'm assuming this is a code snippet rather than what you actually ran.

answered Jan 29, 2012 at 4:02

1 Comment

Yes its just a small piece of a larger program. Adding the main() method seems to have solved my problem. Thank you. Dumb mistake :P
1
import java.util.ArrayList;
class Problem {
public static ArrayList<String> problems = new ArrayList<String>();
public static ArrayList<String> answers = new ArrayList<String>();
private static String question1 = "What is 2+2?";
private static String question2 = "What is the square root of 25";
private static String question3 = "What is the next number in the sequence? {2, 4, 6}"; 
private static String question4 = "What is 8*8?";
public static void main(String [] args) {
String[] temp1 = {question1, question2, question3, question4};
for (String s : temp1)
 problems.add(s);
System.out.println(""+problems);
}

}

answered Jan 29, 2012 at 4:14

Comments

0

Actually, you're missing a method declaration.

Try putting your code inside of a Main method:

public static void main(String[] args) {
 String[] temp1 = {question1, question2, question3, question4};
 for (String s : temp1)
 problems.add(s);
 }
}
answered Jan 29, 2012 at 4:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.