Java Control Statement

Java 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:20 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:
I want example of Control Statement in Java
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...I want example of Control Statement in Java  Hi, I want answer
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... statementJava supports two selection statements, if and switch
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
java if statement
java if statement  If statement in Java
Break statement in java
Break statement in java Break statement in java is used to change the normal control flow of  compound statement like while, do-while , for. Break...; loop. Within a loop if a break statement is encountered then control resume
if else statement in java
if else statement in java  if else statement in java explain with example
Switch Statement with Strings in Java
Switch Statement with Strings in Java  Switch Statement with Strings in Java
if else statement in java
if else statement in java  explain about simple if else statement and complex if else statement in java with an example
Java callable statement
Java callable statement  What is callable statement? Tell me the way to get the callable statement
JAVA statement - Java Beginners
JAVA statement  The import statement is always the first noncomment statement in a Java program file.Is it true?  Hi Friend, No,it is not true.If your class belongs to a package, the package statement should
PHP Control Statement
PHP Control Statement The PHP Conditional statements helps us to perform... to control the flow of the program according to the requirement of the program. In the current tutorial - we'll study two control statements: If..else
conditional statement in java
conditional statement in java  Write a conditional statement in Java   Logical and comparison statements in OOPs are also know as conditional statements. You can learn "How to write conditional statement in Java " from
Java switch statement
Java switch statement  What restrictions are placed on the values of each case of a switch statement
GOTO Statement in java
GOTO Statement in java  I have tried many time to use java goto statement but it never works i search from youtube google but e the example on net are also give compile error. if possible please give me some code with example
If statement in java 7
If statement in java 7 This tutorial describes the if statement in java 7. This is one kind of decision making statement. There are various way to use if statement with else - If Statement :ADS_TO_REPLACE_1  If statement
Java - Continue statement in Java
Java - Continue statement in Java       Continue: The continue statement is used in many programming... and continue statement that the break statement exit control from the loop
Continue statement in java
Continue statement in java In this section we will discuss about continue statement in java. continue is one of the branching statement used in most of the programming languages like C,C++ and java etc. Sometime we need to skip some
Java IF statement problem
Java IF statement problem  Dear Sir/Madam i am using the following code. expected to not have any output but it still showing "welcome".. please help me why it showing "welcome". class c1 { int a=5; while(a>1
ELSE STATEMENT!! - Java Beginners
ELSE STATEMENT!!  Hi! I just want to know why doesn't my else statement works? Else statement is there in the code below which is: else JOptionPane.showMessageDialog(null, n+ " is not in the array!");//doesn't work
Branching Statements in java 7
Branching Statements in java 7 In this section, we will discuss about Branching Statements in java 7. This is one type of control flow statement. Branching... statement is another branching statement provided by the java 7. Its
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
piracy control - Java Beginners
piracy control  how can i make a program that can control piracy in java? piracy control program means that in which we can only run any text file, mp3 file but it cant be copy from there. can u please help me in making that its
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
Control Flow Statements in java 7
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... but you can also control flow of your program with the help of control flow
Java - The switch construct in Java
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
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
Break Statement in java 7
Break Statement in java 7 In this tutorial we will discuss about break statement in java 7. Break Statement : Java facilitate you to break the flow... : If you want to terminate the outer statement java provides the way to do
Continue Statement in java 7
Continue Statement in java 7 In this tutorial we will discuss about continue statement in java 7. Continue Statement : Sometimes you need to skip block... statement in loops.ADS_TO_REPLACE_1 In java 7, Continue statement stops the current
Decision-making Statements in java 7
Decision-making Statements in java 7. This is one type of control flow statement... operator statement. If Statement : If statement is one of the basic control... statement is another decision-making statement used to control the flow of program
Switch Statement example in Java
for understanding more about the switch statement in Java. The following java... Switch Statement example in Java        This is very simple Java program
Java Control Statements
Java Control Statements      ... if The if statement: To start with controlling statements in Java, lets have a recap over... in C++. The if-then statement is the most simpler form of control flow statement
Looping Statements in java 7
Looping Statements in java 7 In this section, we will discuss about Looping Statements in java 7. This is one type of control flow statement. Looping.... It is single line statement. It contains three terms-initialization, condition
Switch case in java
, and java, switch case is used . switch  is a selection control mechanism used to allow the variable to change the control flow of the program. Unlike...Switch case in java In this section we will learn about switch case in java
java program using control statements...
java program using control statements...  public class ControlStatements { public static void main(String args[]){ int x=10; if(x==0){ System.out.println("It is zero"); else if(x>0) System.out.println
Java Break Statement
Java Break Statement      ... is of unlabeled break statement in java. In the program break statement...; Syntax for Break Statement in Java public class Java_Break_Statement
What is the difference between a break statement and a continue statement?
statement? Thanks,   Hello, In Java programming language supports 3... and return statement etc. Other-then Java programming language the break...What is the difference between a break statement and a continue statement
for loop in java example
.style1 { color: #0000FF; } for loop in java example We are going to discuss about for loop in java example. The for loop statement has type loop control statement. We first initialize the variable. After that check
count statement that saves results on a variable in a java bean
count statement that saves results on a variable in a java bean  ...; Statement statement = null; String SQLStr ; String retString...:3306/dev","root","anime"); statement = conn.createStatement
count statement that saves results on a variable in a java bean
count statement that saves results on a variable in a java bean  ...; Statement statement = null; String SQLStr ; String retString...:3306/dev","root","pro"); statement = conn.createStatement
Java Control Statements
Java Control Statements     ... Statements   The control statement are used to controll the flow of execution... and the conditional logic. Java contains the following types of control statements:ADS
Java error unreachable statement
Java error unreachable statement   ... exits the control of the expression from the loop, but continue statement put... a code that help you in understanding a unreachable statement. For this we
For Loop Statement in java 7
For Loop Statement in java 7 In this section, we will discuss about for loop in java 7. This is one type of  loop statement. For Loop Statements : For loop is one way of looping to iterate block of code under certain condition
Continue Statement
the control to the enclosing labeled statement. A continue statement statement must... Continue Statement       The continue statement occurs only inside the iterator statements like while
Java error missing return statement
Java error missing return statement       Java error missing return statement are those error in Java that occurred when a programmer forget to write
Java if else
Java if else       The if-else statement is one of the widely used control flow statements while...*/ } Read more at: http:/www.roseindia.net/java/beginners/IfElse.shtmlADS_TO_REPLACE_2
java icon in control panel - Java Beginners
java icon in control panel  i have a folder icon in the contrl panel instead of a coffee cup when i click on it says not to be found but i have java 6 with 7 downloaded i have windows vista preium
Java Error In comapitable Type in statement - Java Beginners
Java Error In comapitable Type in statement  Here I want to Check...; //Statement st; faculty3() //constructor { j=new JFrame("faculty"); j1...; PreparedStatement st; //Statement st; faculty3(){ j=new JFrame("faculty
Continue and break statement
statement in java program?   The continue statement restart the current loop whereas the break statement causes the control outside the loop. Here... then break statement will call and control will jump outside the current loop
Java - Break statement in java
Java - Break statement in java       The java programming language supports the following types... statement is used in many programming languages such as c, c++, java etc. Some
syntax error in SQL Insert Statement - Java Beginners
Access Driver] Syntax error in INSERT INTO statement. plz Help Me  Hi

Ads