Results 1 to 4 of 4
  1. #1
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Need help with season program

    Following some pseudo code, I created this program.

    Java Code:
    import java.util.Scanner;
    
    public class Season {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    
    		// Define Classes
    		int month;
    		int day;
    		String season = "season";
    
    		// User input
    		System.out.print("Enter a month and day: ");
    		month = in.nextInt();
    		day = in.nextInt();
    
    		// Determine season
    		
    		if (month <= 3){
    			season = "winter";
    		}
    				
    		else if (month <= 6){
    			season = "spring";
    		}
    
    		else if (month <= 9){
    			season = "summer";
    		}
    		
    		else if (month <= 12){
    			season = "fall";
    		}
    
    		if (month % 3 == 0 && day >= 21)
    		{
    			if (season == "winter")
    				season = "spring";
    				}
    			else if (season == "spring"){
    				season = "summer";
    				}
    			else if (season == "summer"){
    				season = "fall";
    				}
    			else {
    				season = "winter";
    				}[/b]
    		
    		// print
    		if (month >= 13 || month < 1 || day > 31 || day <= 0) {
    			System.out.println("Please enter a valid date");
    		} else {
    			System.out.println(season);
    		}
    	}
    }
    It appears to me to be flawless, but it apparently is flawed because the seasons in the output are almost always wrong (except for month 3, for some reason).
    Are there any apparent mistakes you guys can see? I've been at this for quite a few hours and it just doesn't make sense to me...I'm sure it is something simple though.

    UPDATE::
    Wow that was fast, but after I decided to try one more thing, I got it to work!
    I simply had the swirly brackets misplaced on last part.
    changed to
    Java Code:
    if (month % 3 == 0 && day >= 21)
    		{
    			
    			if (season == "winter") {
    				season = "spring";
    				}
    			else if (season == "spring"){
    				season = "summer";
    				}
    			else if (season == "summer"){
    				season = "fall";
    				}
    			else {
    				season = "winter";
    				}
    		}
    Last edited by Aimforthehead; 09-27-2011 at 05:45 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need help with season program

    Never compare Strings with == but instead use the equals or equalsIgnoreCase method. == checks to see if two String objects are one and the same, and you really don't care about that while equals checks if two Strings hold the same chars in the same order, and that's what's important to you.

    So instead of:
    Java Code:
    if (season == "winter") {
    do:
    Java Code:
    if (season.equals("winter")) {
    or:
    Java Code:
    if (season.equalsIgnoreCase("winter")) {

  3. #3
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Need help with season program

    Okay I will change that as well.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need help with season program

    Quote Originally Posted by Aimforthehead View Post
    Wow that was fast, but after I decided to try one more thing, I got it to work!
    I simply had the swirly brackets misplaced on last part.
    I'm glad that you've got it working.

    A good habit to get into is to enclose all if blocks, else blocks, for loops, while loops, shoot -- all loops, blocks and such, in curly braces { and }. This type of habit will save your behind many times in the future.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •