• Register
1 vote
15 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

Please log in or register to answer this question.

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 1 view
1 view
Problem: I am Making a calculator using a switch case. The result I get : Enter a number : 5 Enter another number : 4 What do you want to perform on these numbers? You have entered a wrong action, please try again
asked Mar 23 ummesalma 25.2k points
0 votes
1 answer 3 views
3 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 24 rubelkhan 20.4k points
0 votes
1 answer 9 views
9 views
Problem: Any possible solution will be admitted with thanks: cannot jump from switch statement to this case label c++ .
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 17 views
17 views
Problem: The problem is that variables declared in one case are still visible in the subsequent cases unless an explicit { } block is used, but they will not be initialized because the initialization code belongs to another case.
asked Dec 15, 2020 Mashhoodch 13k points
3 votes
2 answers 1.4K views
1.4K 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’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, 2020 Gavin 15.3k points
0 votes
1 answer 1.5K views
1.5K 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
0 votes
1 answer 5 views
5 views
Problem: i need fix this problem who know about python please help how to use switch case in python
asked Mar 16 Rohit kr 16.2k points
0 votes
1 answer 96 views
96 views
Problem: Hello! I am a new learner. I recently started programming in C#. I learned the control statements in which I came across if, if-else, and switch statements. I tried the following program to have a better understanding of switch statement: using System; class HelloWorld ... label `case default:' I do not know what I have done wrong. Can anybody help me in figuring out what is wrong here?
asked Jan 4 Code Learner 9.2k points
0 votes
1 answer 14 views
14 views
Problem: What is the difference between switch statement and if else statment ? can anyone explain with examples.
asked Dec 23, 2020 prog_learner 2.5k points
0 votes
1 answer 2 views
2 views
Problem: In a switch statement, what will happen if a break statement is omitted?
asked 2 days ago nabin shrestha 2.9k points