Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Rajan Thapa
JOptionPane
2 Answer(s)      3 years and 6 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.

NOTE:

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


Advertisement
View Answers

March 21, 2011 at 12:01 PM


  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]



March 25, 2011 at 4:22 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:
joptionpane validation
joptionpane validation  import javax.swing.*; import java.util.regex.*; public class joptionpane { public static void main(String[] args... help me!i need to validate the user input on the joptionpane,and if the user
joptionpane - Java Beginners
joptionpane  Hi, I need joptionpane validation. I have joptionpane inputdialog textbox.I need to enter the string values only in the textbox for validation. Please send the sample code immediately. Its very urgent
Advertisements
joptionpane - Java Beginners
joptionpane  Hi, I need joptionpane showinputdialog textbox string validation. Please send the sample code for me. Please its very urgent.... Thanks, Valarmathi  Hi Friend, Try the following code: import
joptionpane - Java Beginners
joptionpane   I need joptionpane showinputdialog textbox integer validation. Please send the sample code for me. Please its very urgent.... Thanks, Valarmathi  Hi Friend, Try the following code: import
Array in JOptionPane - Java Beginners
Array in JOptionPane  Hello, I'm having trouble with printing an array in a JOptionPane. I am supposed to make a 2 dimensional int array called BlasTable. I'm supposed to use a method to print the array in a JOptionPane
joptionpane - Java Beginners
joptionpane  Hi, I need to change the JOptionpane foreground text color only. Please send the sample code. JOptionPane.showMessageDialog(createqmbutton, "Succesfully Create the Queue Manager", "Create Queue
Get and Display using JOptionPane
from student, name and age, by using JOptionPane. Then display it. Really
Selection Using JoptionPane
Selection Using JoptionPane  Help, can someone provide me the code using JOptionPane for the following:- Part A 1) Prompt user to make 3 selection i.e 1)Orchid $4.00 2)Rose $2.50 3)Daisy $5.00 or 0 to Quit 2) if user
joptionpane - Java Beginners
joptionpane  Hi, I need to hide the joptionpane message dialogs. import javax.swing.*; import java.util.regex.*; public class ValidateJOptionPane { public static void main(String[]args){ String input
JOptionPane - Java Beginners
and output should be via JOptionPane dialogue boxes. Examples are shown below
Set Color in JOptionPane
Set Color in JOptionPane       In this section, you will learn how to set color in JOptionPane. Now to do... following key value pair, we have set the background color for JOptionPane
Set delay time in JOptionPane
Set delay time in JOptionPane       In this section, you will learn how to set the time after which the message should be displayed using JOptionpane. For this, first of all we prompt
JOptionPane Validation
JOptionPane Validation JOptionPane allow you to create dialog boxes easily... look complex. Here we are going to validate the textfield of JoptionPane dialog. JOptionPane String Validation We have used regular expression that 
Set text color in JoptionPane
Set text color in JoptionPane In this section, you will learn how to set text color in JOptionPane. Now to do this, we have used UIManager class which... color for JOptionPane and background color for JPanel: um.put
Java ComboBox in JOptionPane
Java ComboBox in JOptionPane In this section, you will learn how to create combo box in JOptionPane dialog box. We have created a button to perform an action.On clicking the button, you will get the dialog box with combo box in it. 
Display JList value selected from the JOptionPane
Display JList value selected from the JOptionPane In this section, we are going to select the particular value from the JOptionPane combobox and displayed.... To proceed further, when the user click the button, the JOptionPane with combobox
Java Message Box
of the message box is used for error message. Using JOptionPane class you can create...;. There are  several feature JOptionPane like customizing icon, customizing... on screen. JOptionPane icon let you decide which icon you want to display. You
Show Dialog Box in Java
messages to the user. In this section we will use JOptionPane class to display..., following Message is displayed: Program description: JOptionPane Class: In non... or numeric values but now in the swing application we can use JOptionPane to show
Java Alert Box
JOptionPane class to display the message alert box.  JOptionPane class is available in the javax.swing.*; package. JOptionPane is a swing application we can use JOptionPane to show message and we can use any java application. JOptionPane
Java error icon
; JOptionPane's icon support specify icon to which the dialog displays. We can use four standard icon using JOptionPane icons  like...;new JOptionPane();     JOptionPane.showMessageDialog(null
java swings - Java Beginners
java swings  Hi, I have JoptionPane combobox,in the joptionpane jcombobox have array of values. If i select one value from the joptionpane combobox ,then the selected value will be displayed in the listbox. Please send
java swings - Java Beginners
java swings  Hi, I need the code for how can i set the joptionpane color(background and foreground). can i set the delay time for joptionpane display. Please send the code .its urgent. Thanks, Valarmathi
java swings - Java Beginners
java swings   Hi, I need the code for joptionpane with jcombobox. my requirement is click on add button,one joptionpane will come.from the option pane i need to select the combobox values. Please send the sample code
roob
the JOptionPane and ask the user to enter any number between 1 to 12. Based... and it has 31 days‚??. The output will be shown in the JOptionpane. Now modify...??February has 28 days‚??. Again, the output should be shown in JOptionPane
roob
the JOptionPane and ask the user to enter any number between 1 to 12. Based... and it has 31 days‚??. The output will be shown in the JOptionpane. Now modify...?? it will show ‚??February has 28 days‚??. Again, the output should be shown in JOptionPane
roob
the JOptionPane and ask the user to enter any number between 1 to 12. Based... and it has 31 days‚??. The output will be shown in the JOptionpane. Now modify...?? it will show ‚??February has 28 days‚??. Again, the output should be shown in JOptionPane
java coding
java coding  using swing i want to give an exit button which on clicking ask a msg "JOptionPane" is select "yes" then it should exit or else not....plez help me...... JOptionPane.showConfirmDialog(null,"DO YOU REALLY WANT
JOptionPane.showMessageDialog(this, Ex.getMessage().toString());
with JOPtionPane class, like JOptionPaneERROR_MESSAGE. They are of integer type. So if you write 1, nothing will get happen. But if you will use JOptionPane
customer
object to check the current balance f. Use JOptionPane to allow a user enter a deposit. The program should display the new balance. g. Use JOptionPane to allow
 

 

 

DMCA.com