Difference between revisions of "Days in a Month"
Line 22: | Line 22: | ||
int month; | int month; | ||
</pre> | </pre> | ||
+ | |||
+ | Now that we have a place to store the input, we use JOptionPane.showInputDialog to capture the input from the user: | ||
+ | |||
+ | <pre> | ||
+ | // Get input from user | ||
+ | input = JOptionPane.showInputDialog(null, "Please enter a month in number format (1-12)."); | ||
+ | </pre> | ||
+ | |||
+ | The "raw" text input is now stored in the input String variable. | ||
+ | |||
+ | Next we need to convert this String into an int. This is done with the Integer.parseInt method: | ||
+ | |||
+ | <pre> | ||
+ | // Convert string to integer | ||
+ | month = Integer.parseInt(input); | ||
+ | </pre> | ||
+ | |||
+ | The Integer.parseInt method takes a String and returns an int. In this case, "month" is our int that we declared at the start of the program. The month variable will now contain the number of the month the user entered. If the user did not enter a number; for example entered the word "cat", or even left the input blank, the program will throw a NumberFormatException and then shut down. This is okay for our program. When you become a more experienced programmer you will learn how to handle exceptions. | ||
|SolutionCode=<pre> | |SolutionCode=<pre> |
Revision as of 14:40, 3 April 2010
Back to the Program-A-Day homepage
ProblemCreate a program that allows the user to enter a month, and then outputs the number of days in that month. For simplicity sake, ask the user for the number of the month rather than the string. Also, assume there are 28 days in February. Use JOptionPane.showInputDialog to capture the input, and System.out.println to generate the output. Make sure the user enters a valid month (1-12). If they enter a number that is not in that range, output "Invalid Month". If the user enters a non-integer, let the program crash.
|
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionLike most programs, this program has three phases: Input, calculation and output. First, we declare the variables that we will use to store the input. We need a String to record the "raw" text input from the user, and an integer (int) to store the number of the month when it is converted from the "raw" text input: // These variables are used for input String input; int month; Now that we have a place to store the input, we use JOptionPane.showInputDialog to capture the input from the user: // Get input from user input = JOptionPane.showInputDialog(null, "Please enter a month in number format (1-12)."); The "raw" text input is now stored in the input String variable. Next we need to convert this String into an int. This is done with the Integer.parseInt method: // Convert string to integer month = Integer.parseInt(input); The Integer.parseInt method takes a String and returns an int. In this case, "month" is our int that we declared at the start of the program. The month variable will now contain the number of the month the user entered. If the user did not enter a number; for example entered the word "cat", or even left the input blank, the program will throw a NumberFormatException and then shut down. This is okay for our program. When you become a more experienced programmer you will learn how to handle exceptions. | ||
Code |