0

So here's the homework question:

"Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day.

Ex: If the input is:

April 11 the output is:

Spring In addition, check if the string and int are valid (an actual month and day).

Ex: If the input is:

Blue 65 the output is:

Invalid "

My code is as follows:

'''

    String inputMonth;
    int inputDay;

    inputMonth = scnr.next();

    inputDay = scnr.nextInt();

  if( ((inputMonth == "March") || (inputMonth == "march")) && ((inputDay >= 20) && (inputDay <= 31))){
     System.out.println("Spring");  
  }
  else if( ((inputMonth == "April") || (inputMonth == "april")) && ((inputDay >= 1) && (inputDay <= 30))){
     System.out.println("Spring");  
  }
    else if( ((inputMonth == "May") || (inputMonth == "may")) && ((inputDay >= 1) && (inputDay <= 31))){
     System.out.println("Spring");  
  }
    else if( ((inputMonth == "June") || (inputMonth == "june")) && ((inputDay >= 1) && (inputDay <= 20))){
     System.out.println("Spring");  
  }
    else if( ((inputMonth == "June") || (inputMonth == "june")) && ((inputDay >= 21) && (inputDay <= 30))){
     System.out.println("Summer");  
  }
    else if( ((inputMonth == "July") || (inputMonth == "july")) && ((inputDay >= 1) && (inputDay <= 31))){
     System.out.println("Summer");  
  }
    else if( ((inputMonth == "August") || (inputMonth == "august")) && ((inputDay >= 1) && (inputDay <= 31))){
     System.out.println("Summer");  
  }
    else if( ((inputMonth == "September") || (inputMonth == "september")) && ((inputDay >= 1) && (inputDay <= 21))){
     System.out.println("Summer");  
  }
    else if( ((inputMonth == "September") || (inputMonth == "september")) && ((inputDay >= 22) && (inputDay <= 30))){
     System.out.println("Autumn");  
  }
  else if( ((inputMonth == "October") || (inputMonth == "october")) && ((inputDay >= 1) && (inputDay <= 31))){
     System.out.println("Autumn");  
  }
  else if( ((inputMonth == "November") || (inputMonth == "november")) && ((inputDay >= 22) && (inputDay <= 30))){
     System.out.println("Autumn");  
  }
  else if( ((inputMonth == "December") || (inputMonth == "december")) && ((inputDay >= 1) && (inputDay <= 20))){
     System.out.println("Autumn");  
  }
  else if( ((inputMonth == "December") || (inputMonth == "december")) && ((inputDay >= 21) && (inputDay <= 31))){
     System.out.println("Winter");  
  }
   else if( ((inputMonth == "January") || (inputMonth == "january")) && ((inputDay >= 1) && (inputDay <= 31))){
     System.out.println("Winter");  
  }
  else if( ((inputMonth == "February") || (inputMonth == "february")) && ((inputDay >= 1) && (inputDay <= 29))){
     System.out.println("Winter");  
  }
  else if( ((inputMonth == "March") || (inputMonth == "march")) && ((inputDay >= 1) && (inputDay <= 19))){
     System.out.println("Winter");  
  }
  else{
   System.out.println("Invalid");  
  }

'''

I believe the problem is that it wont read both the string and the integer correctly but I'm not exactly sure why.

Also I know there's probably a shorter way to do this but I don't know how yet, if anyone would like to help me with that too it would be infinitely appreciated.

Thank you in advance

  • Also, I think you've misinterpreted your assignment. The example show month and day on a single line, but your program seems to expect the values to be entered separately. You should post a second question in that case, since the answer will be substantially different from this one. – MarsAtomic Mar 17 '20 at 20:00
  • 1
    Check out the Java API for the String class and look for the different types of `equals` methods. It will make your task easier. – WJS Mar 17 '20 at 20:03
  • The original poster was just asking for help, I don't know why these questions just cannot be answered to help educate people. The op was using equivelance of the object identity, the hash value stored in memory. To fix the code you need to compare the string value. instead of inputMonth == "april" it should be inputMonth.equals("april") I also sugggest doing toLowerCase on the input – Branden May 09 '21 at 17:18

1 Answers1

0

I don't know if this version makes more sense to you or not.

I put the month names, seasons, and date ranges in arrays. Normally, I would have created a class to hold the season name and date range, but I used arrays to keep it simpler.

I commented on the more obscure sections of code. I hope this helps.

package com.ggl.testing;

import java.util.Scanner;

public class Seasons {

    static String[] months = { "January", "February", "March", "April", 
            "May", "June", "July", "August", "September",
            "October", "November", "December" };

    // Winter has two ranges to make the comparisons easier
    static String[] seasons = { "Spring", "Summer", "Autumn", "Winter", 
            "Winter" };

    // Month and day to start the season, month and day to end the season
    // Winter has two ranges to make the comparisons easier
    // The month number is zero based (0 - 11)
    static int[][] ranges = { { 2, 20, 5, 20 }, { 5, 21, 8, 21 }, 
            { 8, 22, 11, 20 }, { 11, 21, 11, 31 },
            { 0, 1, 2, 19 } };

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] values = getInput(scanner);
        if (values[0] < 0 || values[1] < 0) {
            System.out.println("Invalid");
        } else {
            System.out.println(getSeason(values));
        }
        scanner.close();
    }

    static int[] getInput(Scanner scanner) {
        int[] output = new int[2];
        System.out.print("Type the month and day: ");
        String s = scanner.nextLine().trim();
        String[] parts = s.split(" ");
        output[0] = getMonthIndex(parts[0]);
        output[1] = getDay(parts[1]);
        return output;
    }

    static int getMonthIndex(String month) {
        for (int i = 0; i < months.length; i++) {
            if (month.toLowerCase().equals(months[i].toLowerCase())) {
                return i;
            }
        }
        return -1;
    }

    static int getDay(String number) {
        try {
            int value = Integer.valueOf(number);
            // TODO Check last day of a particular month
            if (value < 1 || value > 31) {
                return -1;
            } else {
                return value;
            }
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    static String getSeason(int[] values) {
        for (int i = 0; i < ranges.length; i++) {
            if ((ranges[i][0] == values[0]) && (ranges[i][1] <= values[1])) {
                return seasons[i];
            } else if ((values[0] == ranges[i][2]) && (values[1] <= ranges[i][3])) {
                return seasons[i];
            } else if ((ranges[i][0] < values[0]) && values[0] < ranges[i][2]) {
                return seasons[i];
            }
        }
        return "Invalid";
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111