• Register
1 vote
9 views

Problem:

Hello Kodlogs,

I am feeling good to find a helpful website like this. So many nice, and friendly people are here.

Let me come to the point and put my problem here. I am new to java programming and trying to learn new things every day. I just found a new thing called Switch-Statement.

So, I read about it thoroughly and tried to write a code using the Switch-Statement, but the problem is it returns me an error (the default case must be specified in a switch statement.) instead of my expected output.

Here is my program snippet.

public class Practice { 
    public static void main(String[] args){ 
        int weather = 5; 
        String dayString; 
        switch (weather) { 
        case 1: 
            dayString = "Sunny"; 
            break; 
        case 2: 
            dayString = "Rainy"; 
            break; 
        case 3: 
            dayString = "Windy"; 
            break; 
        case 4: 
            dayString = "Snowy"; 
            break; 
        case 5: 
            dayString = "Cloudy"; 
            break;  
        } 
        System.out.println(dayString); 
    } 
} 

Please shed some light on this. Thanks.

 

12 7 7
15,250 points

1 Answer

1 vote
 
Best answer

Solution:

Well, in a switch-statement you have to have a default case in your program.

ublic class Practice { 
    public static void main(String[] args){ 
        int weather = 5; 
        String dayString; 
        switch (weather) { 
        case 1: 
            dayString = "Sunny"; 
            break; 
        case 2: 
            dayString = "Rainy"; 
            break; 
        case 3: 
            dayString = "Windy"; 
            break; 
        case 4: 
            dayString = "Snowy"; 
            break; 
        case 5: 
            dayString = "Cloudy"; 
            break;  
        default: 
            dayString = "It is an ordinary day!"; 
            break; 
        } 
        System.out.println(dayString); 
    } 
} 

Here, in every case, the dayString will be checked and if it doesn’t match any cases it will print for the default case. So, in a nutshell, a switch-statement can’t be boolean like if-else. I just added a default case in your program and it’s running fine now.
Thanks.

13 9 6
94,240 points

Related questions

0 votes
1 answer 641 views
641 views
Problem : I am much more familiar with the enums in C# but it seems like enums in the Java is a quite different. Currently I am trying to use the switch statement @ enums in my below code but I always face the below error no matter what I am doing. The error I ... myenumExample //... switch (myenumExample) { case MyAnotherClass.MyEnum.MYVALUE_A: { <-- facing error on this line //.. break; } } } }
asked Dec 25, 2019 alecxe 7.5k points
3 votes
2 answers 632 views
632 views
Problem: Getting an error while I was working with a switch statement in c++. I do have a 200 lines code that I can&rsquo;t share with you for obvious reason. I am sharing the switch-statement block that produces the error in my program. The error I am getting is: cannot jump ... { case 1: int i = 42; dostuff(i); break; case 2: dostuff(i*2); } Any possible solution will be admitted with thanks.
asked Mar 25 Gavin 15.3k points
0 votes
1 answer 10 views
10 views
Explain with an Example?
asked Sep 24 jagruthi 560 points
0 votes
1 answer 5 views
5 views asked Oct 2 miki 1k points
0 votes
1 answer 139 views
139 views
Problem: The ________ statement is used to make simple decisions in Java? A) do/while B) for C) branch D) if
asked Mar 9 ArifulIslam 5.7k points