0

So for more context you can look at my last post, but I feel like I went about asking the question all wrong.

What I'm given is one input on one line: month day eg April 11

What I need to do is, based on the month and day given, determine the season. So I need to first get the String inputMonth; and get the int inputDay; from the single string providing both. I am conveniently provided with the months and days of the seasons which resulted in me writing the following:

          //I know that the inputDay is wrong
          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");  
  }

My issue is that I can't figure out how to determine the month and the day separately from that one input. Whenever I run this I always end up getting "Invalid".
Some people on my last post were showing different ways to go about this but to be honest I'm not familiar with arrays yet (That's next lesson).
Thanks in advance guys.

  • Strings are compared with the equals() method. For examples, "September".equals(inputMonth); – NomadMaker Mar 18 '20 at 01:34
  • Second, would use String line = scnr.nextLine(); Then I would use split to break this into two strings. String[] words = line.split(" "); String month = words[0]; int day = Integer.parseInt(words[1]); – NomadMaker Mar 18 '20 at 01:38

0 Answers0