-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"};
  
  
}

}

  • 2
    What is your question? – Basil Bourque Oct 10 '21 at 03: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: [ask]. – Ole V.V. Oct 10 '21 at 04:34
  • Googling for "java initialize array of strings" gave me the duplink. The top two answers answer your question. – Stephen C Oct 10 '21 at 04:36
  • Your answer is here: [Help - Syntax error on token "=", Name expected after token](https://community.oracle.com/tech/developers/discussion/1261128/help-syntax-error-on-token-name-expected-after-token). – Ole V.V. Oct 10 '21 at 04: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). – Ole V.V. Oct 10 '21 at 04:42
  • You may need this array for an exercise that specifically excludes better alternatives (in which case the answer below gives you what you need). For a real-world program do not create such an array. Java knows the days of the week. If you need an array of them get it from `DayOfWeek.values()`. And if and when you need to print the name to your user, get it from the `getDisplayName` method of each element of the array you got. – Ole V.V. Oct 10 '21 at 04:50
  • If you can find a better >>StackOverflow<< duplink, please add it. But it is a matter of semantics. Do you interpret this as a "how do I initialize an array?" ... as per the title, or "how do I fix the syntax error in this code?" ... which the OP didn't bother to tell anyone about. Frankly, I don't really think it matters. It is a poor question (for many reasons) and hopefully the Roomba will clear it out. – Stephen C Oct 10 '21 at 05:18
  • @StephenC Well said. And definitely the latter (in spite of the ambiguity). – Ole V.V. Oct 10 '21 at 06:04

1 Answers1

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"
};
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249