JOptionPage program to take user input

JOptionPage program to take user input

  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:24 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:
How to take input by user using jDialogue and use that input
How to take input by user using jDialogue and use that input  I am using NetBeans i have a 'Search by employee id' button . I want when user click this button a dialogue box appear which take employee_id as input from user
Write a program that takes the marks of user as input
Write a program that takes the marks of user as input  Write a program that takes the marks of user as input. If marks are greater then 60 the program should display "Great, you have passed the course", else the program should
Advertisements
Take input in batch file
Take input in batch file  How to take input in batch file? Thanks   Hi, You can use the following line of code in your batch file: set... entered by user. Thanks
escaping user input in php
escaping user input in php  Is it possible to escape the user input while submitting data into database in PHP
Create a Program that Calculates Input
Create a Program that Calculates Input   Create a program that takes user input and does a calculation with it, then prints the results back for the user. This could include like a sales tax calculator, or a tip calculator
Java User Input
Java User Input  I am using Scanner class for user Input. Scanner s = new Scanner(System.in); String name = s.next(); but i am unable to store full name in name variable...how i can store full name plsss reply   use
calculation after if. user input
user input????   My apologies good evening peoples Once again I am...calculation after if. user input  System.out.print ("please select... have tried putting the nested if I press 1 for apples then it just makes me take
java program to take backup for the folders
java program to take backup for the folders  java program to take backup for the folders   Have a look at the following link: Java Copy one folder to another
user input to database
user input to database  /* * Studentinfo2App.java */ package studentinfo2; import java.sql.Connection; import java.sql.DriverManager; import... in my database user input from my applet does not show
user input to database
user input to database  /* * Studentinfo2App.java */ package studentinfo2; import java.sql.Connection; import java.sql.DriverManager; import... in my database user input from my applet does not show
user input to database
user input to database  /* * Studentinfo2App.java */ package studentinfo2; import java.sql.Connection; import java.sql.DriverManager; import... in my database user input from my applet does not show
button to accept user input
the radiobutton and spinner.The user input does not show on my Studentinfo.mdb...button to accept user input  private void jButton1ActionPerformed... that accepts firstname, lastname and gender from user and insert to database
How to read user input in C++
How to read user input in C++  How can i get an input from the users in C++?   You can use the standard input "cin" to get the input from user.. For example: int age; cin >> age
Read user input in Java using Scanner
input from console. Our sample program asks user to enter name and then age, user... class object. Now let's make a simple program to reading user input from...Example of of reading user input in Java using Scanner In this section we
program to input marks
program to input marks  program to input marks ,name in computer application 7 annual fees to be paid .Decide the scholarship amount as per the given criteria Marks Scholarship(%)on Annual Fees =90
Factorial Program in Java
. System.in is used to take the input from system/user at run time. InputStreamReader reads the input from user and then keep it into a buffer. As Java reads... If the input is out of the array than the program will display the result as "Array out
user input in java - Java Beginners
user input in java   i am trying to write a code to compute the average of 5 numbers but i dont know what to do for the program to accept user input  Hi import java.util.*; public class AverageExample { public
java user input - Java Beginners
java user input  how to input from keyboard? please give a example.  Hi Friend, There are lot of ways to input data from keyboard.You...:****** try{ System.out.print("Enter String:"); BufferedReader input = new
error in taking input from user.
error in taking input from user.  //i m having problem inputting the array from user.my program is import java.io.*; class bubble { public static...); } } } //above program is taking only 4 values. please suggest solution soon. tell me
how to take input table name in jsp to create table in mysql?
how to take input table name in jsp to create table in mysql?  how to take input table name in jsp to create table in mysql?   Hello Friend...; <form method="post"> Enter Table Name:<input type="text" name="table
Java get User Input
Java get User Input       In this section, you will learn how to get the user input from... that will communicate with the user at the command line and returns the user input. We have
Save the input data until user get changed
Save the input data until user get changed  I would like to know how to store the give input data and perform the calculations even after re-opening the program. I am developing a college library management system, i would like
Save the input data until user get changed
Save the input data until user get changed  I would like to know how to store the give input data and perform the calculations even after re-opening the program. I am developing a college library management system, i would like
Save the input data until user get changed
Save the input data until user get changed  I would like to know how to store the give input data and perform the calculations even after re-opening the program. I am developing a college library management system, i would like
Design a registration.jsp page with input for user registration details
Design a registration.jsp page with input for user registration details  Design a registration.jsp page with input for user registration details like firstname, lastname, choose username, choose password, confirm password. After
XP Bowling Game User input help
XP Bowling Game User input help  I was asked to do the XP Bowling Example as an assignment. I have the programme passing all tests, but now I need to make the code accept input from a user and display the scores/frames
Managing Multiple User Input Data in an Array (or ArrayList)
Managing Multiple User Input Data in an Array (or ArrayList)  Hey... record alphabetically (2) If user chooses choice1, the input data is stored in an ARRAY (or ARRAYLIST) until the user chooses to stop inputting data. Kindly
Display PHP clock with user input date and time
Display PHP clock with user input date and time  The following PHP code displays a clock with current date and time. I want the clock to receive user input date and time. How can this be done? <?php date<em>
input
input  a java program that will exchange the last names of two students that were input by a user
Validation on input xml in java program - XML
Validation on input xml in java program  Hi.. i am accepting xml data as input which I have to use in my program. i have to validate this input xml against some schema using xml beans and NOT by using sax or dom parser. I am
Getting ISD code when user input country name
Getting ISD code when user input country name  I want to create program which user needs by typing Country name he will get ISD code of that country... code, from that list i have to read the file and when user input any country name
input type dialog box take text and search a list and focus that text - JSP-Servlet
input type dialog box take text and search a list and focus that text  i have a button..when i press it will prompt a dialog box with a text box... then it will search a list which contains data in the same page and if found
write a program to create a user defined
write a program to create a user defined   plz send me code   Hi Friend, Please specify what do you want to create?ADS_TO_REPLACE_1 Thanks
iPhone Input Field, iPhone Input Field Tutorial
iPhone Input Field Generally, input fields are used to take input from the users.  In iPhone application we can use Text Field to take the input. ...; In this example we are going to create a small application that will take the input
Change the user input to integer
Change the user input to integer   ... an integer value after the compilation of a program and force the JVM to ask for an input, then we should use Integer.parseInt(string str).  As we know
java program to take the backup of folders and copy the folder in different location without deleting the existing one
java program to take the backup of folders and copy the folder in different location without deleting the existing one  java program to take the backup of folders and copy the folder in different location without deleting
Calculator program in Java
Calculator program in Java is used by programmer to add, subtract, multiply or divide two integer numbers that are input by system. In the given example a class "calculator" is used. System.in takes the input from the system/user at run
write a program in java Adding up the subscript(st,nd,rd,th) to the number of days in a input string
write a program in java Adding up the subscript(st,nd,rd,th) to the number of days in a input string    write a program in java Adding up the subscript(st,nd,rd,th) to the number of days in a input string ex:If a user enters
simple program question
simple program question  how do i get a java program to take an input from the user and have it terminate by a period and not by pressing enter
write a java program for pagination which will be configured by end user
write a java program for pagination which will be configured by end user  write a java program for pagination which will be configured by end user
write a program which asks the user for two int values and calculates their sum.
write a program which asks the user for two int values and calculates their sum.  The title says it all, an example is (1,2) > 3
need help with two dimensional array that takes input from user and bubble sorts and selections sorts
need help with two dimensional array that takes input from user and bubble sorts and selections sorts  I can get both of the sorts figured out but i... how many elements are going to be input by the user?   Here is a code
write a java program for pagination which will be configured by end user
write a java program for pagination which will be configured by end user  write a java program for pagination which will be configured by end user  
write a java program to print marklist of n students. input Register number, name and marks of three subjects.
write a java program to print marklist of "n " students. input Register number, name and marks of three subjects.  write a java program to print marklist of "n " students. input Register number, name and marks of three subjects
Servlet Error Message based on user input
Servlet Error Message based on user input   ... to check the user input against database and display the message to the user. This example illustrate how to ensure that user input is correct by validating
program to create student report using java applet,read the input using text boxesand generate the grades..?
program to create student report using java applet,read the input using text boxesand generate the grades..?   sir plz give me java applet codes for above question
HTTPClient Delete Method program ( for REST API to delete a user )
HTTPClient Delete Method program ( for REST API to delete a user )  HI Friends, I am trying a HTTP delete method for creating REST API in java. can any one provide me a HTTPClient Delete method program for reference. Please
Array and input
[ ]={1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26}; i want to ask user to input one of the above numbers then i want to print the array without the number user input. how will i do tht
ModuleNotFoundError: No module named 'take'
ModuleNotFoundError: No module named 'take'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'take' How to remove the ModuleNotFoundError: No module named 'take' error
ModuleNotFoundError: No module named 'take'
ModuleNotFoundError: No module named 'take'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'take' How to remove the ModuleNotFoundError: No module named 'take' error

Ads