Difference between revisions of "Days in a Month"

From CompSciWiki
Jump to: navigation, search
(EDIT: Touched up problem a bit, changed a sentence in discussion to remove an incorrect semi-colon)
m (Changed to codeblocks)
 
Line 17: Line 17:
 
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:
 
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:
  
<pre>
+
{{CodeBlock|Code=
 
// These variables are used for input
 
// These variables are used for input
 
String input;
 
String input;
 
int month;
 
int month;
</pre>
+
}}
  
 
Now that we have a place to store the input, we use JOptionPane.showInputDialog to capture the input from the user:
 
Now that we have a place to store the input, we use JOptionPane.showInputDialog to capture the input from the user:
  
<pre>
+
{{CodeBlock|Code=
 
// Get input from user
 
// Get input from user
 
input = JOptionPane.showInputDialog(null, "Please enter a month in number format (1-12).");
 
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.
 
The "raw" text input is now stored in the input String variable.
Line 34: Line 34:
 
Next we need to convert this String into an int.  This is done with the Integer.parseInt method:
 
Next we need to convert this String into an int.  This is done with the Integer.parseInt method:
  
<pre>
+
{{CodeBlock|Code=
 
// Convert string to integer
 
// Convert string to integer
 
month = Integer.parseInt(input);
 
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 if the word "cat" was entered, or if the input was left 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 these exceptions.
 
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 if the word "cat" was entered, or if the input was left 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 these exceptions.
Line 43: Line 43:
 
We've completed the first phase of the program: Gathering the input.  We will now go on to the second and third phases.  The calculation portion is done with a series of if...else if...else statements.  The output is done within these statements:
 
We've completed the first phase of the program: Gathering the input.  We will now go on to the second and third phases.  The calculation portion is done with a series of if...else if...else statements.  The output is done within these statements:
  
<pre>
+
{{CodeBlock|Code=
 
if( month == 1 || month == 3 || month == 5 || month == 7 ||
 
if( month == 1 || month == 3 || month == 5 || month == 7 ||
 
month == 8 || month == 10 || month == 12 ) // Months that have 31 days
 
month == 8 || month == 10 || month == 12 ) // Months that have 31 days
Line 61: Line 61:
 
System.out.println("Invalid Month.");
 
System.out.println("Invalid Month.");
 
}
 
}
</pre>
+
}}
  
 
No matter what, one of these System.out.println statements will be executed.  The program will first check if the month entered is one of the months that have 31 days.  Then it will check if it is a month that has 30 days; then 28 days.  Finally, if the number entered does not correspond with a valid month, it will print "Invalid Month".
 
No matter what, one of these System.out.println statements will be executed.  The program will first check if the month entered is one of the months that have 31 days.  Then it will check if it is a month that has 30 days; then 28 days.  Finally, if the number entered does not correspond with a valid month, it will print "Invalid Month".

Latest revision as of 15:05, 4 December 2011

Back to the Program-A-Day homepage

Problem

Create a program that allows the user to enter a month, and then outputs the number of days in that month. For simplicity's sake, ask the user for the number of the month rather than its name. Also, assume there are always 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.


For example, if they want to know how many days there are in the month of January, the user would enter the number 1 and receive an answer of 31.

To complete this problem, you should have a good understanding of If, if-else and conditional statements as well as JOptionPane Methods.

 

If Statements and Named Constants

Wiki sign01.jpg

Solution

Like 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 if the word "cat" was entered, or if the input was left 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 these exceptions.

We've completed the first phase of the program: Gathering the input. We will now go on to the second and third phases. The calculation portion is done with a series of if...else if...else statements. The output is done within these statements:

 if( month == 1 

No matter what, one of these System.out.println statements will be executed. The program will first check if the month entered is one of the months that have 31 days. Then it will check if it is a month that has 30 days; then 28 days. Finally, if the number entered does not correspond with a valid month, it will print "Invalid Month".

For the entire code solution, see below.

Code

Solution Code

Back to the Program-A-Day homepage