Reputation: 43
I already written the program and ran it but I don't know how to prevent both println statements to show up when I run it with an invalid digit. It's basically a program in which you input the month & day (in numerical value) and the output would be the season. Here's the code:
public class Solstice {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int month;
int day;
String season = "seasons";
System.out.println("Enter a month: ");
month = in.nextInt();
System.out.println("Enter a day: ");
day = in.nextInt();
String winter = "winter";
String spring = "spring";
String summer = "summer";
String fall = "fall";
System.out.println("Month="+ month +" Day= "+day);
if (month <= 3)
{
season = winter;
}
else if (month <= 6)
{
season = spring;
}
else if (month <= 9)
{
season = summer;
}
else if (month <= 12)
{
season = fall;
} else {
System.out.println("The value is invalid.");
}
System.out.println(season);
}
}
Upvotes: 1
Views: 73
Reputation: 21586
I suggest to encapsulate the if statements. You will not need to work with null
. And you will only get output for valid seasons.
if (month > 12 || month < 1)
{
season = invalid;
} else {
if (month > 9)
{
season = fall;
}
else if (month > 6)
{
season = summer;
}
else if (month > 3)
{
season = spring;
}
else
{
season = winter;
}
System.out.println("season is ", season);
}
Upvotes: 0
Reputation: 4425
The simplest way would be test as follows. Otherwise you would set a true false flag and test it at the end to determine what to print. Note that system is set to invalid as a default and therefore the error printout would be season is invalid
Another way of doing this would be a switch statement with the appropriate cases and setting the default to make season invalid. That way one would not need to do all the elseif statements. However, I am showing the if elseif in order to keep it like the original question.
System.out.println("Enter a month: ");
month = in.nextInt();
System.out.println("Enter a day: ");
day = in.nextInt();
String winter = "winter";
String spring = "spring";
String summer = "summer";
String fall = "fall";
String invalid = "invalid";
String season = invalid; // Set the default to invalid
System.out.println("Month="+ month +" Day= "+day);
if (month > 12 || month < 1)
{
season = invalid;
}
elseif (month > 9)
{
season = fall;
}
if (month > 6)
{
season = summer;
}
else if (month > 3)
{
season = spring;
}
else
{
season = winter;
}
System.out.println("season is ", season);
Upvotes: 1
Reputation: 1679
write:
String season = null;
and then check before the last if:
if(season == null){
//print invalid
} else {
//print corresponding season
}
Upvotes: 2
Reputation: 726639
Set the season
to null
, and check for null
before printing. Remove the last else
if (season != null) {
System.out.println(season);
} else {
System.out.println("The value is invalid.");
}
Upvotes: 0
Reputation: 1249
One way could be:
boolean invalid = false;
if (month <= 3)
{
season = winter;
}
else if (month <= 6)
{
season = spring;
}
else if (month <= 9)
{
season = summer;
}
else if (month <= 12)
{
season = fall;
} else {
invalid = true;
}
if(invalid){
System.out.println("The value is invalid.");
} else {
System.out.println(season);
}
The only thing you have to do is to remember whether the date was invalid. Therefore, you need a boolean flag invalid
.
Upvotes: 1