Rajan Thapa
Java Control Statement
1 Answer(s)      5 years and 9 months ago
Posted in : Java Interview Questions
  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.


[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);
            case 1:

            case 2:
            String str=JOptionPane.showInputDialog(null,"Is this a leap year?");

            case 3: 

            case 4:

            case 5: 

            case 6:

            case 7: 

            case 8: 

            case 9: 

            case 10:

            case 11: 

            case 12: 

        JOptionPane.showMessageDialog(null,"The Month you entered is "+getMonthName(num-1)+" and it has "+days+" days");

Related Tutorials/Questions & Answers:
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
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
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
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
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  if else statement in java explain with example
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
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
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
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
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 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
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 :  If statement contains one boolean
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
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
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
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
Branching Statements in java 7
Branching Statements in java 7. This is one type of control flow statement. Branching... statement provided by the java 7. Its functionality to stop the current... statement is to exits the current method and it returns the flow of your control
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 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
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
PIRACY CONTROL IN PEER TO PEER NETWORK in JAVA  Can i get any source code in this Project Topic "Piracy Control in peer to peer Network
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... java provides the way to do this by using labeled break statement. You can jump
Switch Statement example in Java
more about the switch statement in Java. The following java program tells you... Switch Statement example in Java        This is very simple Java program
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