Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please assist me with this? I can get this to work using a switch st

ID: 3662779 • Letter: C

Question

Can someone please assist me with this? I can get this to work using a switch statement but I am struggling when it comes to using an array or arraylist to make this Java Coding project work. Any help would be greatly appreciated!

Given the poem:

Thirty days hath September
April, June, and November
All the rest have thirty-one
with February's 28 to make it fun.
Leap Year happening one in four, Gives February one day more.

Write a program that does the following:

1. Prompts the user for a month string (January, February, ...). Validate the input month string against an array of month's, and prompt the user again if the String entered is not a valid month.

2. Prompts the user for a year (e.g. 2016).  

3. For months with 31 days, output the phrase "all the rest have thirty-one". For September, the phrase "Thirty days hath September". For April, June, and November the phrase "April, June, and November". For February, if the year is not a leap year, output the phrase "with February's 28 to make it fun.". If the use did supply a leap year (and the user supplied February as the month), output the phrase "Leap Year happening one in four, Gives February one day more."

4.Note. Since this chapter covers Arrays and ArrayList objects, I expect your solution to utilize these facilities. The specifics of this is up to you (after all, this is your program :) ), so I am making specific requirements regarding this, other than you show learnings from chapter 7.

Please make sure you comment your code thoroughly.

The code should be nicely formatted and should use proper variables.

Explanation / Answer

I have not used switch because before java 7,java didn't support switch case for string inputs

Months.java

import java.io.*;
public class Months {

   public static void main(String[] args)throws IOException
   {
       //Created a array of string with month names to validate the input month string
       String months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
      
       //Created a array of strings to hold the lines of poem,each line as one string element of array
       String poem[]=new String[5];
      
       poem[0]="Thirty days hath September";
       poem[1]="April, June, and November";
       poem[2]="All the rest have thirty-one";
       poem[3]="with February's 28 to make it fun";
       poem[4]="Leap Year happening one in four, Gives February one day more.";
      
      
       InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
String month="";
//Prompting user for a valid month name,until a valid month is entered loop will go on
while(true)
{
   System.out.println("Enter a valid month name");
month=br.readLine();
//Validated month name if it is a valid month than only proceed else reprompt
   if(validateMonth(month,months))
   {
       break;
   }
}
  
  
System.out.println("Enter the year");
int year=Integer.parseInt(br.readLine());
if(month.equalsIgnoreCase("January"))
{
   System.out.println(poem[2]);
}
else if(month.equalsIgnoreCase("February"))
{
   if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
   {
       System.out.println(poem[4]);
   }
   else
   {
       System.out.println(poem[3]);
   }
}
else if(month.equalsIgnoreCase("March"))
{
   System.out.println(poem[2]);
}
else if(month.equalsIgnoreCase("April"))
{
   System.out.println(poem[1]);
}
else if(month.equalsIgnoreCase("May"))
{
   System.out.println(poem[2]);
}
else if(month.equalsIgnoreCase("June"))
{
   System.out.println(poem[1]);
}
else if(month.equalsIgnoreCase("July"))
{
   System.out.println(poem[2]);
}
else if(month.equalsIgnoreCase("August"))
{
   System.out.println(poem[2]);
}
else if(month.equalsIgnoreCase("September"))
{
   System.out.println(poem[0]);
}
else if(month.equalsIgnoreCase("October"))
{
   System.out.println(poem[2]);
}
else if(month.equalsIgnoreCase("November"))
{
   System.out.println(poem[1]);
}
else if(month.equalsIgnoreCase("December"))
{
   System.out.println(poem[2]);
}
  
}

   //For validating if month name is valid
   private static boolean validateMonth(String month,String months[])
   {
       boolean result=false;
       for(int i=0;i<12;++i)
       {
           if(month.equalsIgnoreCase(months[i]))
           {
               result=true;
               break;
           }
       }
       return result;
   }

}