1

Hi I am (very) new to java and I come from a c++ background and I am getting this error:

Array-Lists-In-java.java:3: error: '{' expected
public class Array-Lists-In-java{
                  ^

and here is my code:

    import java.util.ArrayList;
    
    public class Array-Lists-In-java {
    
        public static void main(String[] args)
        {
            ArrayList<String> grocery = {"Onions","Carrots","Olives", "olive oil"};
            
            printArray(grocery);
            
            grocery.add("Tomatoes");
            
            printArray(grocery);
            
        }
        
        public static void printArray(ArrayList<String> list)
        {
            for(int i = 0; i < list.size(); i++)
            {
                System.out.println(list.get(i));
            }
        }
        
    }

what am I doing wrong??pls correct me where i am going wrong it would be really helpful

thanks

  • 4
    rename Array-Lists-In-java to ArrayListsInJava and take a look at the naming conventions for java classes – funky Sep 24 '20 at 16:19
  • 2
    Hyphens aren't valid characters for class names or variable names. stack post: https://stackoverflow.com/questions/65475/valid-characters-in-a-java-class-name – George Sep 24 '20 at 16:20
  • 6
    Does this answer your question? [Valid characters in a Java class name](https://stackoverflow.com/questions/65475/valid-characters-in-a-java-class-name) – George Sep 24 '20 at 16:21
  • 2
    I'd expect this to be invalid in C++ too, mind you... – Jon Skeet Sep 24 '20 at 16:27

1 Answers1

0

Hey python_is_good_cpp_is_best! I think you are missing or have an extra flower bracket somewhere, remember each flower bracket should have a buddy! Try counting how many open flower brackets you have, and how many closed flower brackets you have. if they are the same amount, remember to use proper naming convention, programming languages dont support names having '-' cause they think you want to do subtraction, you have a '-' in your class name, try replacing '-' with underscores '_' it should work properly! thankyou!

Sha7r
  • 145
  • 2
  • 11