Hey everyone! I'm diving into Java and I've been practicing with the switch statement in class. However, I ran into a weird issue while trying to use System.out.println. I'm working in Eclipse and the example is about the days of the week. Here's the code I wrote:
```java
public class tutorial {
public static void main(String[] args) {
String day = "Friday";
switch(day) {
case "Monday":
System.out.println("Today is Monday.");
break;
// and so on with the other days.
}
}
}
```
The problem is that in my program, it seems like it can't read the System.out.println part because it's not showing the right colors for text and break statements. When I hover over it, I get a message saying I need to put a String or println with String, but that isn't what we did in the class. I even tried rewriting the code, but it doesn't solve the issue.
I've made a new class and it works fine if I just type Sysout, but when I try to paste the switch code to the new class, it doesn't change the others, even though the original case works. My friend suggested it might be a syntax issue, but I'm using Ctrl+Space to save time, so I doubt it. I'd really like to know what's going on and how to fix it!
1 Answer
Are you getting any compiler errors? Those can often give you a more reliable clue than just looking at colors in the IDE. It might help to check your error logs.
Yeah! I found an error message when I tried to run the program. It said: "Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error, insert OH". I just realized that the issue was I didn't include a closing brace at the end of my switch statement, which is why it wouldn't compile! Thanks for the nudge; that made me look closer at it.