I have an assignment I am doing for my class. I have to write to ask for a year, and then the month by the input of the first three letters of the month. It will output the follows statement "February 2000 has 29 days" from what I am asked for the project. it is supposed to be case sensitive. not sure how to do that. can anyone point me in the right direction? also does this look good or am I being sloppy.
package test.project;
import java.util.Scanner;
/**
*
* @author XXXXXXX
*/
public class TESTPROJECT {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = input.nextInt();
System.out.print("Enter month (first three letters with the first letter in upper case): ");
String month = input.next();
//months
if (month.equals("Jan")){
System.out.print("Jan " + year);
System.out.print(" has 31 days.");
}
if (month.equals("Feb")){
System.out.print("Feb " + year);
//LEAP YEAR FORMULA
if (year%4==0&&(year%100!=0||year%400==0))
{System.out.print(" has 29 days.");
if (year%4!=0&&(year%100==0||year%400!=0))
{System.out.print(" has 28 days.");}}
}
if (month.equals("Mar")){
System.out.print("Mar " + year);
System.out.print(" has 31 days.");
}
if (month.equals("Apr")){
System.out.print("Apr " + year);
System.out.print(" has 30 days.");
}
if (month.equals("May")){
System.out.print("May " + year);
System.out.print(" has 31 days.");
}
if (month.equals("Jun")){
System.out.print("Jun " + year);
System.out.print(" has 30 days.");
}
if (month.equals("Jul")){
System.out.print("Jul " + year);
System.out.print(" has 31 days.");
}
if (month.equals("Aug")){
System.out.print("August " + year);
System.out.print(" has 31 days.");
}
if (month.equals("Sep")){
System.out.print("Sep " + year);
System.out.print(" has 30 days.");
}
if (month.equals("Oct")){
System.out.print("Oct " + year);
System.out.print(" has 31 days.");
}
if (month.equals("Nov")){
System.out.print("Nov " + year);
System.out.print(" has 30 days.");
}
if (month.equals("Dec")){
System.out.print("Dec " + year);
System.out.print(" has 31 days.");
}
}
}
Just assuming you only mean the first letter of the month is case sensitive, you could include this in each for loop
if (month.equals("Feb") || month.equals("feb")){
System.out.print("Feb " + year);
This would work assuming it is only the first letter that is case sensitive. The ||
mean if the input means this "Feb" or this "feb". You could keep using it on and on for each different example until their is no other possible options for feb i.e. FEB, FeB, fEb etc...