Switch control Statement

Switch control Statement

  1. Write a Java Program with file name SectionB.java using Switch control Statement

The program will use the JOptionPane and ask the user to enter any number between 1 to 12.

Based on the input, then the program will show how many days in that month. For example if the user enters 1, the program will show â??The Month you entered is January and it has 31 daysâ??. The output will be shown in the JOptionpane.

  1. Now modify the program; if the user enters â??2â?? the program will ask the user if it is a leap year. If the answer from the user is â??Yesâ?? it will show the message â??February has 29 daysâ??, if the response from the user is â??Noâ?? it will show â??February has 28 daysâ??. Again, the output should be shown in JOptionPane.

NOTE:

[In this program use try and catch block to handle any unacceptable and invalid input from the user]

View Answers

March 25, 2011 at 4:23 PM

import javax.swing.*;
import java.util.*;
import java.text.*;

class SectionB{
    static String getMonthName(int m) {
    String month = "invalid";
    DateFormatSymbols dfs = new DateFormatSymbols();
    String[] months = dfs.getMonths();
    if (m >= 0 && m <= 11 ) {
        month = months[m];
    }
    return month;
}

    public static void main(String[] args){
            int days=0;
        String st=JOptionPane.showInputDialog(null,"Enter number between 1 and 12: ");
        int num=Integer.parseInt(st);
        switch(num){
            case 1:
            days=31;
            break;

            case 2:
            String str=JOptionPane.showInputDialog(null,"Is this a leap year?");
            if(str.equalsIgnoreCase("yes")){
            days=29;
            }
            else{
            days=28;
            }
            break;

            case 3: 
            days=31;
            break;

            case 4:
            days=30;
            break;

            case 5: 
            days=31;
            break;

            case 6:
            days=30;
            break;

            case 7: 
            days=31;
            break;

            case 8: 
            days=31;
            break;

            case 9: 
            days=30;
            break;

            case 10:
            days=31;
            break;

            case 11: 
            days=30;
            break;

            case 12: 
            days=31;
            break;

            default:
            System.out.println("Invalid!");
            System.exit(0);
        }
        JOptionPane.showMessageDialog(null,"The Month you entered is "+getMonthName(num-1)+" and it has "+days+" days");
    }
}









Related Tutorials/Questions & Answers:
switch statement
switch statement   i want to write a java program that computes Fibonacci,factorial,string reversal and ackerman using switch case to run as a single program
The Switch statement
. To avoid this we can use Switch statements in Java. The switch statement is used... of a variable or expression. The switch statement in Java is the best way to test.... Here is the general form of switch statement: switch (expression){ case 1
Advertisements
Java Switch Statement
Java Switch Statement       In java, switch is one of the control statement which turns the normal flow control of the program as per conditions. It's like if-else statement but it can
Switch Statement
switch case statement. The program displays the name of the days according to user...Switch Statement  How we can use switch case in java program ?   Note:-Switch case provides built-in multiway decision statement.It
How to use switch statement in jsp code
How to use switch statement in jsp code       switch is a type of control statement used to control the flow of program execution. Its body is known as a switch block
Switch Statement with Strings in Java
Switch Statement with Strings in Java  Switch Statement with Strings in Java
Java switch statement
Java switch statement  What restrictions are placed on the values of each case of a switch statement
Switch statement in PHP
Switch statement in PHP  HII, Explain about switch statement in PHP?   hello,ADS_TO_REPLACE_1 Switch statement is executed line by line... of the switch expression. It does execute the statement until the end of the block
What is the difference between an if statement and a switch statement?
What is the difference between an if statement and a switch statement?   Hi, What is the difference between an if statement and a switch statement... values). Whereas the Switch statement offers you multiple choice of option from
Switch case in java
, and java, switch case is used . switch  is a selection control mechanism used... if statementswitch allow the user to select a option from multiple options. switch statement have multiple number of possible execution path. It work
Switch Statement example in Java
Switch Statement example in Java    ... for implementing the switch statement. Here, you will learn how to use the switch statement... for understanding more about the switch statement in Java. The following java
C Break with Switch statement
C Break with Switch statement   ... with Switch-Case in C. The switch case statements allows to control complex... the user to enter any number. The expression defined in the switch statement finds
Java - The switch construct in Java
;    Switch is the control statement in java which... flow of control quits from the Switch block whenever break statement occurs.... In this Program you will see that how to use the switch statement
Java Control Statement
Java Control Statement Control flow statement is a statement that is used to break the sequential execution of statement. control flow statement, controls... statement :  Java supports two selection statements, if and switch
I want example of Control Statement in Java
I want example of Control Statement in Java  Hi, I want answer... statement in Java is used to control the flow of execution of program. Here is the Examples of Control Statement in Java: Java Control Statements Java Control
PHP Switch Case
Switch Case Control Structure: Almost every modern language supports switch case control structure. It is similar to if statements. Switch case is useful... the statements until it found a break statement or the end of the switch case block
Switch Statement in java 7
Switch Statement in java 7 This tutorial describes the if statement in java 7. This is one kind of decision making statement. Switch Statements : The switch statement is used when you want to test many statements based on some
i need to replace this if statement code with switch case code
i need to replace this if statement code with switch case code   i need to replace this code with switch case for(int i=0;i<100;i++){ if((i...){ switch(i/j){ case 3: System.out.println ("java
Find Capital using Switch-Case statement
Find Capital using Switch-Case statement In this Java Tutorial section, we are going to find the capital of our states using Switch-Case statement. For this, we have created two arrays i.e. array of states and array of capitals. Then we
Strings in Switch statement
Strings in Switch statement In this section, you will learn about strings..., you can pass string as expression in switch statement. The switch statement... have matched string. The comparison of string in switch statement is case
PHP Control Statement
PHP Control Statement The PHP Conditional statements helps us to perform... and switch case. ADS_TO_REPLACE_1 Example: php-control-stmt.html <!DOCTYPE html... to control the flow of the program according to the requirement of the program
Java Control Statements
in C++. The if-then statement is the most simpler form of control flow statement... is executed if "if" statement is false.    Switch... statement is used to force program control back to the top of a loop. 
Control Statments
in C++. The if-then statement is the most simpler form of control flow statement... Control Statments      ... top to bottom. We will learn how the different kinds of statement have
Switch
statements. To avoid this we can use Switch statements in Java. The switch statement... of a variable or expression. The switch statement in Java is the best way to test.... Here is the general form of switch statement: switch (expression){ case 1
Java Control Statements
Statements   The control statement are used to controll the flow of execution...: This is a control statement to execute a single statement or a block of code, when... } Switch Statement: This is an easier implementation to the if-else statements
Switch Case in Java
if-then and if-then-else statements. As switch statement allows in-numerous possible execution paths... the default statement will be executed. A switch block is the body of a switch... case label A statement in the switch block can be labeled with one or more case
for statement
for statement  for(int i=0;i<5;i++); { system.out.println("The value of i is :"+i); } if i end for statement what will be the output   got the answer.. it displays only the last iteration that is "The value of i
Break statement in java
control flow of  compound statement like while, do-while , for. Break... is also used in switch case. Break statement allow a force termination of  loop. Within a loop if a break statement is encountered then control resume
Break Statement in JSP
are using the switch statement under which we will use break statement. The for loop...Break Statement in JSP          The use of break statement is to escape
Flow Control
switch case
switch case  program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong
switch case
switch case  program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong
Switch databases
Switch databases  How do you inter-change between relational databases without code changes
switch Java Keyword
:  -- The switch statement in java language enables the user to select... statement at the end of each case block to exit from the switch statement. -- If the break statement is lacking in the switch case then the flow of execution will flow
programes on switch
=input.nextInt(); switch(choice){ case 1
switch case instead of if else
switch case instead of if else  How to write code in switch case instead of if else in Java
Control Flow Statements in java 7
Decision making- ADS_TO_REPLACE_2 If statement switch statement...Control Flow Statements in java 7 In this section we will discuss Control Flow Statements in java 7. This is one type of Language Fundamentals. Control
ModuleNotFoundError: No module named 'switch'
ModuleNotFoundError: No module named 'switch'  Hi, My Python... 'switch' How to remove the ModuleNotFoundError: No module named 'switch'... to install padas library. You can install switch python with following command
ModuleNotFoundError: No module named 'switch'
ModuleNotFoundError: No module named 'switch'  Hi, My Python... 'switch' How to remove the ModuleNotFoundError: No module named 'switch'... to install padas library. You can install switch python with following command
Statement block
Statement block  What is the purpose of a statement block
What is the difference between a break statement and a continue statement?
What is the difference between a break statement and a continue statement?   Hi, What is the difference between a break statement and a continue statement? Thanks,   Hello, In Java programming language supports 3
using switch,break and for loop
using switch,break and for loop  generate a 10 digit number and display the length of longest increasing series
using switch and break
using switch and break  generate a 10 digit number and display the length of longest increasing series
if statement in php
if statement in php  Correct way of writing if statement in PHP
java if statement
java if statement  If statement in Java
How to break statement in JSP
How to break statement in JSP  Hi, I am writing JSP page and there is for loop inside it. How to break statement in JSP? Thanks   Hi, I JSP you can use the break statement to break a loop. Here is simple example
How to break statement in JSP
How to break statement in JSP  Hi, I am writing JSP page and there is for loop inside it. How to break statement in JSP? Thanks   Hi, I JSP you can use the break statement to break a loop. Here is simple example
How to break statement in JSP
How to break statement in JSP  Hi, I am writing JSP page and there is for loop inside it. How to break statement in JSP? Thanks   Hi, I JSP you can use the break statement to break a loop. Here is simple example
How to break statement in JSP
How to break statement in JSP  Hi, I am writing JSP page and there is for loop inside it. How to break statement in JSP? Thanks   Hi, I JSP you can use the break statement to break a loop. Here is simple example
Switch case + condition??
Switch case + condition??  I try to use a switch case with condition but it doesn't display anything have look: ......public void commandAction...(); } } else { List down = (List)display.getCurrent(); switch

Ads