0

I have a problem with eclipse. I am trying to make a program that generate random pizza toppings Five in total. All of them are put in 4 different arrays. I try to put in a array list and then to send through a button in a JtexArea. In a console program variant i randomised the arrays and send first element with sysout. It works. Now i try to make work with a desktop GUI

package nzk.nazakthul.pizza;

import java.util.*;

public class TopinguriClass {

        //  Arrays
        String[] salami = { "muschi", "bacon", "salam de vara", "salam de     sibiu","sunca presata", "salam corizo" };

        // arraylist
        List<String> list = new ArrayList<String>();

}

if i put the next code after list object i get 2 errors. one says i need to have a } that closes the class and the other says that expect something after ; on the ArrayList line

for (String x:salami)
   list.add(x);

am i missing something?

I have written already the code for GUI If there is a possibility to send an array directly to a text area would be ok.

1
  • 2
    Also do you mean list.add? Commented Jan 26, 2014 at 4:25

2 Answers 2

3

The additional statements need to be in a code block such as a constructor, method or instance initializer rather than the class block

public TopinguriClass() {
    for (String x : salami) {
        list.add(x);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

dosen't work. Anyway i put in a method all code and error disappeared.
That's what's meant by code block. You realise that the above snippet should be located within in a class not on its own?
Is works. still have minor problems with output (formatting problems ) but is's works
0

you need to give list.add() not as add.list

List is not Generic and you cannot give like List<String>

Do like this

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

for (String x:salami)

list.add(x);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.