AVA: My code encounters the following problem. Could you fix it? The purpose of this code is to input the date and output the season. import java.util.Scanner; public class Seasons { public static String getMonth(Scanner console){ System.out.print("Enter the month: "); String month = console.nextLine().trim(); return month; } public static int getDay(Scanner console){ System.out.println("Enter day: "); int day = console.nextInt(); return day;
JAVA: My code encounters the following problem. Could you fix it? The purpose of this code is to input the date and output the season.
import java.util.Scanner;
public class Seasons {
public static String getMonth(Scanner console){
System.out.print("Enter the month: ");
String month = console.nextLine().trim();
return month;
}
public static int getDay(Scanner console){
System.out.println("Enter day: ");
int day = console.nextInt();
return day;
}
public static boolean ValidInput(String InputMonth, int InputDay){
String[] monthName = {"January","Feburary","March","April","May","June","July","August",
"September","October","November","December"};
int[] day = {31,28,31,30,31,30,31,31,30,31,30,31};
if(InputDay<0){
return false;
}
for(int i=0 ; i<12; i++){
if(InputMonth.equalsIgnoreCase(monthName[i])){
if(InputDay <= day[i])
return true;
else
return false;
}
}
return false;
}
public static String calcSeason(String InputMonth, int InputDay){
String season = "";
if(InputMonth.equalsIgnoreCase("December")){
if(InputDay >= 21)
season = "winter";
else
season = "Fall";
}
if(InputMonth.equalsIgnoreCase("January") || InputMonth.equalsIgnoreCase("Feburary"))
season = "winter";
if(InputMonth.equalsIgnoreCase("March")) {
if(InputDay <= 19)
season = "winter";
else
season = "spring";
}
if(InputMonth.equalsIgnoreCase("April") || InputMonth.equalsIgnoreCase("May"))
season = "spring";
if(InputMonth.equalsIgnoreCase("June")) {
if(InputDay <= 20)
season = "spring";
else
season = "summer";
}
if(InputMonth.equalsIgnoreCase("July") || InputMonth.equalsIgnoreCase("August"))
season = "summer";
if(InputMonth.equalsIgnoreCase("September")){
if(InputDay <= 20)
season = "summer";
else
season = "fall";
}
if(InputMonth.equalsIgnoreCase("October") || InputMonth.equalsIgnoreCase("November"))
season = "fall";
return season;
}
public static void displaySeason(String season){
System.out.println("This date occurs in the "+season+".");
}
public static void main(String[] args) {
//create object of Scanner class to get input from user
Scanner console = new Scanner(System.in);
//variable declaration
String month;
int day;
//function calling and set month and date variables
month = getMonth(console);
day = getDay(console);
if(ValidInput(month,day)){
String season = calcSeason(month,day);
displaySeason(season);
}
else
System.out.println("Invalid date. Program cannot continue.");
}
}



Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 3 images









