-2

public class MyClass { public static void main(String args[]) {

//Create an array of Strings which are initialized to the 7 days of the week using a while-loop, print all the contents of the array. (do the same for do-while and for loop)

  String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
  
  
}

}

8
  • 2
    What is your question? Commented Oct 10, 2021 at 3:54
  • Welcome to Stack Overflow. You are correct if what you meant was that there is a subtle error in your code. I didn’t see it until I read the last line of the correct answer by Elliot Frisch. Therefore: when asking about an error in your program, always paste the error message into your question. It will allow more people to understand the question and the error and hence answer. See more here: How to Ask. Commented Oct 10, 2021 at 4:34
  • Googling for "java initialize array of strings" gave me the duplink. The top two answers answer your question. Commented Oct 10, 2021 at 4:36
  • Your answer is here: Help - Syntax error on token "=", Name expected after token. Commented Oct 10, 2021 at 4:38
  • @StephenC I uphold that it’s a very poor original for this question. I admit it’s hard to be sure because the question isn’t there, but I believe that this question is about a very specific syntax error which is not covered in the linked question. And I also agree that closing this question as a duplicate is likely to be more helpful than closing as needing debugging details or downright unclear (which would arguably be more correct and deserved). Commented Oct 10, 2021 at 4:42

1 Answer 1

3

tl;dr use {} not ().

You forgot to ask a question, but the array initialization syntax is

String days[] = new String[] {
        "Monday", "Tuesday", "Wednesday", "Thursday", 
        "Friday", "Saturday", "Sunday"
};

And can be shortened to

String days[] = {
        "Monday", "Tuesday", "Wednesday", "Thursday", 
        "Friday", "Saturday", "Sunday"
};

And the prefered way (in Java) has the array type with the name of the class. So,

String[] days = {
        "Monday", "Tuesday", "Wednesday", "Thursday", 
        "Friday", "Saturday", "Sunday"
};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.