Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Case Switch Help?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Case Switch Help?

    import java.util.Scanner;
     
    public class BirthdayEdit {
     
    	public static void main (String arg[])
    	{
    		Scanner sc = new Scanner(System.in);
     
    		int birthMonth = sc.nextInt();
    		int birthDay = sc.nextInt();
    		int birthYear = (sc.nextInt() - 1900);
    		double birthYearRemainder = birthYear % 4;
    		int leap;
    		int total;
     
    		int birthMonthAdd;
    		switch (birthMonth) {
    			case 1: birthMonthAdd = 1; break;
    			case 2: birthMonthAdd = 4; break;
    			case 3: birthMonthAdd = 4; break;
    			case 4: birthMonthAdd = 0; break;
    			case 5: birthMonthAdd = 2; break;
    			case 6: birthMonthAdd = 5; break;
    			case 7: birthMonthAdd = 0; break;
    			case 8: birthMonthAdd = 3; break;
    			case 9: birthMonthAdd = 6; break;
    			case 10: birthMonthAdd = 1; break;
    			case 11: birthMonthAdd = 4; break;
    			case 12: birthMonthAdd = 6; break;
    		}
    		if (birthYearRemainder==0) {
    			leap = -1;
    		} else {
    			leap = 0;
    		}
    		total = ((birthMonthAdd + birthDay + (birthYear / 4) + birthYear + leap)%7);
    		String day;
    		if (total == 0) {
    		    day = "Saturday";
    		} else if (total == 1) {
    			day = "Sunday";
    			System.out.println("You were born on" + day);
    		} else if (total == 2) {
    			day = "Monday";
    			System.out.println("You were born on" + day);
    		} else if (total == 3) {
    			day = "Tuesday";
    			System.out.println("You were born on" + day);
    		} else if (total == 4) {
    			day = "Wednesday";
    			System.out.println("You were born on" + day);
    		} else if (total == 5) {
    			day = "Thursday";
    			System.out.println("You were born on" + day);
    		} else if (total == 6) {
    			day = "Friday";
    			System.out.println("You were born on" + day);
    		} else if (total < 0) {
    			System.out.println("An error has occurred.");
    		}
        }
    }

    Trying to design a program that tells you the day you were born. I'm getting this error:
    error: variable birthMonthAdd might not have been initialized
    total = ((birthMonthAdd + birthDay + (birthYear / 4) + birthYear + leap)%7);
    I've tried moving variables around everywhere, but I can't get it to work.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Case Switch Help?

    Thats a very basic mistake, happens to everyone. Change
    int birthMonthAdd;
    to
    int birthMonthAdd = 0;
    It has to do with the compiler not knowing that it will actually get initialized in the switch statement

  3. The Following User Says Thank You to Tjstretch For This Useful Post:

    xionyus (October 19th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Case Switch Help?

    Quote Originally Posted by Tjstretch View Post
    Thats a very basic mistake, happens to everyone. Change
    int birthMonthAdd;
    to
    int birthMonthAdd = 0;
    It has to do with the compiler not knowing that it will actually get initialized in the switch statement
    Gaah! Thank you so much, it helped me lots. :]
    If you're still there I'm wondering if there will be a way to make an error code so that if my teacher types in Feburary 30th, it would give him an error. I really can't think of anything to put for it. That's only if you'd like to help again though. My program works now thanks to you! ;D

    import java.util.Scanner;
     
    public class BirthdayEdit {
     
    	public static void main (String arg[])
    	{
    		Scanner sc = new Scanner(System.in);
     
    		int birthMonth = sc.nextInt();
    		int birthDay = sc.nextInt();
    		int birthYear = (sc.nextInt() - 1900);
    		double birthYearRemainder = birthYear % 4;
    		int leap;
    		int total;
     
    		int birthMonthAdd = 0;
    		switch (birthMonth) {
    			case 1: birthMonthAdd = 1; break;
    			case 2: birthMonthAdd = 4; break;
    			case 3: birthMonthAdd = 4; break;
    			case 4: birthMonthAdd = 0; break;
    			case 5: birthMonthAdd = 2; break;
    			case 6: birthMonthAdd = 5; break;
    			case 7: birthMonthAdd = 0; break;
    			case 8: birthMonthAdd = 3; break;
    			case 9: birthMonthAdd = 6; break;
    			case 10: birthMonthAdd = 1; break;
    			case 11: birthMonthAdd = 4; break;
    			case 12: birthMonthAdd = 6; break;
    		}
    		if (birthYearRemainder==0) {
    			leap = -1;
    		} else {
    			leap = 0;
    		}
    		total = ((birthMonthAdd + birthDay + (birthYear / 4) + birthYear + leap)%7);
    		String day;
    		if (birthMonth > 12) {
    		System.out.println("Please enter a month number 1-12.");
    		} if (birthMonth <= 0) {
    		System.out.println("Please enter a month number 1-12.");
    		} if (birthYear < 0) {
    		System.out.println("The year you entered is earlier than the twentieth century.");
    		} if (total == 0) {
    		    day = "Saturday";
    		    System.out.println("You were born on a " + day);
    		} else if (total == 1) {
    			day = "Sunday";
    			System.out.println("You were born on a " + day);
    		} else if (total == 2) {
    			day = "Monday";
    			System.out.println("You were born on a " + day);
    		} else if (total == 3) {
    			day = "Tuesday";
    			System.out.println("You were born on a " + day);
    		} else if (total == 4) {
    			day = "Wednesday";
    			System.out.println("You were born on a " + day);
    		} else if (total == 5) {
    			day = "Thursday";
    			System.out.println("You were born on a " + day);
    		} else if (total == 6) {
    			day = "Friday";
    			System.out.println("You were born on a " + day);
    		} else if (total < 0) {
    			System.out.println("An error has occured.");
    		}
        }
    }

  5. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Case Switch Help?

    You're Welcome, as to your request:

    Throwing an error:
    throw new AssertionError("Maximum 29 days in Febuary");

    Reviewed your code, you should use a switch statement, then use one println. It just looks neater.

    It would look like
    switch(total)
    {
      case 0:
        day = "Saturday";
        break;
      case 1:
        day = "Sunday";
        break;
      case 2:
        day = "Monday";
        break;
      case 3:
        day = "Tuesday";
        break;
      case 4:
        day = "Wednesday";
        break;
      case 5:
        day = "Thursday";
        break;
      case 6:
        day = "Friday";
        break;
      default:
        System.err.println("An error occurred");
        System.exit(0);
    }
    System.out.println("You were born on a "+day);
    Instead of all the else-ifs, however you would have to change
    String day;
    to
    String day = null;
    Last edited by Tjstretch; October 19th, 2011 at 07:10 PM. Reason: Added highlights

  6. The Following User Says Thank You to Tjstretch For This Useful Post:

    xionyus (October 19th, 2011)

  7. #5
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Case Switch Help?

    Quote Originally Posted by Tjstretch View Post
    You're Welcome, as to your request:

    Throwing an error:
    throw new AssertionError("Maximum 29 days in Febuary");

    Reviewed your code, you should use a switch statement, then use one println. It just looks neater.

    It would look like
    switch(total)
    {
      case 0:
        day = "Saturday";
        break;
      case 1:
        day = "Sunday";
        break;
      case 2:
        day = "Monday";
        break;
      case 3:
        day = "Tuesday";
        break;
      case 4:
        day = "Wednesday";
        break;
      case 5:
        day = "Thursday";
        break;
      case 6:
        day = "Friday";
        break;
      default:
        System.err.println("An error occurred");
        System.exit(0);
    }
    System.out.println("You were born on a "+day);
    Instead of all the else-ifs, however you would have to change
    String day;
    to
    String day = null;
    It made my code much more cleaner! Thanks.
    One thing can be break the program though.
    If I enter a month number over 12, or a negative, then it will still input a day. I know I could fix it with else ifs, but anyway to do it in the way you posted?

  8. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Case Switch Help?

    Oh, thats what you meant. Just use a while statement and an if statement, like so:
    int birthMonth = sc.nextInt();
    while(birthMonth > 12 || birthMonth < 0)
    {
      System.out.println("Invalid birth month, please input a valid number");
      birthMonth = sc.nextInt();
    }

    However, it would be best to use Integer.valueOf(sc.nextLine()), because sometimes nextInt() causes problems. I find it best just to create a helper method called getInt(sc);, like so:
    /**
     * Safely aquires an int from a scanner
     */
    private static int getInt(Scanner sc)
    {
      int num = Integer.MIN_VALUE;
      while(num == Integer.MIN_VALUE)
      {
        try
        {
          String line = sc.getLine();
          if(line.equalsIgnoreCase("exit");
             System.exit(0);
          num = Integer.valueOf(line);
        }catch(NumberFormatException e)
        {
          System.err.println("Please input a valid number");
        }
      }
    }

    For getting a valid int, use
    int birthMonth = 0;
    int birthDay = 0;
    int birthYear = 0;
     
    while(birthMonth < 1 || birthMonth > 12)
    {
      System.out.println("What is your birth month? (Must be between 1 and 12)");
      birthMonth = getInt(sc);
    }
    //You could use a long switch statement to make sure its right, but I'm going to assume
    //the highest value is 31.. you could do it if you want, I'm just to lazy atm.
    while(birthDay < 1 || birthDay > 31)
    {
      System.out.println("What is your birth day? (Must be between 1 and 31)");
      birthDay = getInt(sc);
    }
    System.out.println("What is your birth year?");
    birthYear = getInt(sc);
    Last edited by Tjstretch; October 19th, 2011 at 07:31 PM.

  9. #7
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Case Switch Help?

    Sorry if I'm getting over your head, I can break all the code down and explain it if you want
    Also, it will make your code look a little less to-the-point, because you have to check everything for validity.
    Last edited by Tjstretch; October 19th, 2011 at 07:42 PM.

  10. #8
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Case Switch Help?

    Quote Originally Posted by Tjstretch View Post
    Sorry if I'm getting over your head, I can break all the code down and explain it if you want
    Also, it will make your code look a little less to-the-point, because you have to check everything for validity.
    It's not really over my head, I've been coding and stuff since I was about nine, I learned HTML and CSS from neopets. x3
    And I code permissions for MineCraft.
    It's just the fact that the first program I created for this same topic worked, but it wasn't what the teacher wanted. He wanted case statements, and I was parsing. I was gone from school for two weeks and his instructions aren't always the best. Just yesterday he was talking about how you have to be "creative" with coding, but nope. I gotta recode my entire program. LOL.
    Anyways, I just added a couple of else if statements to the end.

    import java.util.Scanner;
     
    public class BirthdayEdit {
     
    	public static void main (String arg[])
    	{
    		Scanner sc = new Scanner(System.in);
    		int birthMonth = sc.nextInt();
    		int birthDay = sc.nextInt();
    		int birthYear = (sc.nextInt() - 1900);
    		double birthYearRemainder = birthYear % 4;
    		int leap;
    		int total;
    		int birthMonthAdd = 0;
    		switch (birthMonth) {
    			case 1: birthMonthAdd = 1; break;
    			case 2: birthMonthAdd = 4; break;
    			case 3: birthMonthAdd = 4; break;
    			case 4: birthMonthAdd = 0; break;
    			case 5: birthMonthAdd = 2; break;
    			case 6: birthMonthAdd = 5; break;
    			case 7: birthMonthAdd = 0; break;
    			case 8: birthMonthAdd = 3; break;
    			case 9: birthMonthAdd = 6; break;
    			case 10: birthMonthAdd = 1; break;
    			case 11: birthMonthAdd = 4; break;
    			case 12: birthMonthAdd = 6; break;
    		}
    		if (birthYearRemainder==0) {
    			leap = -1;
    		} else {
    			leap = 0;
    		}
    		total = ((birthMonthAdd + birthDay + (birthYear / 4) + birthYear + leap)%7);
    		String day = null;
    		switch(total) {
            case 0:
            day = "Saturday";
            break;
            case 1:
            day = "Sunday";
            break;
            case 2:
            day = "Monday";
            break;
            case 3:
            day = "Tuesday";
            break;
            case 4:
            day = "Wednesday";
            break;
            case 5:
            day = "Thursday";
            break;
            case 6:
            day = "Friday";
            break;
            default:
            System.err.println("An error occurred.");
            System.exit(0);
    		}
    		if(birthMonth > 12 || birthMonth < 0) {
            System.out.println("An error occurred.");
            } else if (birthYear > 101 || birthYear < 1) {
            System.out.println("An error occurred.");
            } else {
            System.out.println("You were born on a " +day);
            }
        }
    }

    Ended up with that. Works well for me. The only thing that I can't think of a way to counter is if he asks me to put in February 30th.
    Also, do you know a way to make the program loop until I put "stop" or something to make the program complete?

  11. #9
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Case Switch Help?

    Ok, 2 things. You should change
    int birthMonth = sc.nextInt();
    int birthDay = sc.nextInt();
    int birthYear = (sc.nextInt() - 1900);
    to
    System.out.println("What is your birth month?");
    int birthMonth = sc.nextInt();
    System.out.println("What is your birth day?");
    int birthDay = sc.nextInt();
    System.out.println("What is your birth year?");
    int birthYear = (sc.nextInt() - 1900);

    Then, wrap everything after
    Scanner sc = new Scanner(System.in)
    with a big while statement waiting for a boolean. It's easier to do then explain.
    While statements link is here
    import java.util.Scanner;
     
    public class BirthdayEdit {
     
      public static void main (String arg[])
      {
        Scanner sc = new Scanner(System.in);
        boolean cont = true;
        while(cont)
        {
          System.out.println("What is your birth month?");
          int birthMonth = sc.nextInt();
          System.out.println("What is your birth day?");
          int birthDay = sc.nextInt();
          System.out.println("What is your birth year?");
          int birthYear = (sc.nextInt() - 1900);
          double birthYearRemainder = birthYear % 4;
          int leap;
          int total;
          int birthMonthAdd = 0;
          switch (birthMonth) {
            case 1: birthMonthAdd = 1; break;
            case 2: birthMonthAdd = 4; break;
            case 3: birthMonthAdd = 4; break;
            case 4: birthMonthAdd = 0; break;
            case 5: birthMonthAdd = 2; break;
            case 6: birthMonthAdd = 5; break;
            case 7: birthMonthAdd = 0; break;
            case 8: birthMonthAdd = 3; break;
            case 9: birthMonthAdd = 6; break;
            case 10: birthMonthAdd = 1; break;
            case 11: birthMonthAdd = 4; break;
            case 12: birthMonthAdd = 6; break;
          }
          if (birthYearRemainder==0) {
            leap = -1;
          } else {
            leap = 0;
          }
          total = ((birthMonthAdd + birthDay + (birthYear / 4) + birthYear + leap)%7);
          String day = null;
          switch(total) {
            case 0:
              day = "Saturday";
              break;
            case 1:
              day = "Sunday";
              break;
            case 2:
              day = "Monday";
              break;
            case 3:
              day = "Tuesday";
              break;
            case 4:
              day = "Wednesday";
              break;
            case 5:
              day = "Thursday";
              break;
            case 6:
              day = "Friday";
              break;
            default:
              System.err.println("An error occurred.");
              System.exit(0);
          }
          if(birthMonth > 12 || birthMonth < 0) {
            System.out.println("An error occurred.");
          } else if (birthYear > 101 || birthYear < 1) {
            System.out.println("An error occurred.");
          } else {
            System.out.println("You were born on a " +day);
          }
          System.out.println("Continue?");
          cont = !in.nextLine().equalsIgnoreCase("stop");
        }
      }
    }

  12. #10
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Case Switch Help?

    As for checking if its not Febuary 30th, add this with all your other validity checks
    else if(birthMonth == 2 && birthDay == 30)
    {
      System.out.println("Invalid day!");
      break;
    }

  13. #11
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Case Switch Help?

    Quote Originally Posted by Tjstretch View Post
    Ok, 2 things. You should change
    int birthMonth = sc.nextInt();
    int birthDay = sc.nextInt();
    int birthYear = (sc.nextInt() - 1900);
    to
    System.out.println("What is your birth month?");
    int birthMonth = sc.nextInt();
    System.out.println("What is your birth day?");
    int birthDay = sc.nextInt();
    System.out.println("What is your birth year?");
    int birthYear = (sc.nextInt() - 1900);

    Then, wrap everything after
    Scanner sc = new Scanner(System.in)
    with a big while statement waiting for a boolean. It's easier to do then explain.
    While statements link is here
    import java.util.Scanner;
     
    public class BirthdayEdit {
     
      public static void main (String arg[])
      {
        Scanner sc = new Scanner(System.in);
        boolean cont = true;
        while(cont)
        {
          System.out.println("What is your birth month?");
          int birthMonth = sc.nextInt();
          System.out.println("What is your birth day?");
          int birthDay = sc.nextInt();
          System.out.println("What is your birth year?");
          int birthYear = (sc.nextInt() - 1900);
          double birthYearRemainder = birthYear % 4;
          int leap;
          int total;
          int birthMonthAdd = 0;
          switch (birthMonth) {
            case 1: birthMonthAdd = 1; break;
            case 2: birthMonthAdd = 4; break;
            case 3: birthMonthAdd = 4; break;
            case 4: birthMonthAdd = 0; break;
            case 5: birthMonthAdd = 2; break;
            case 6: birthMonthAdd = 5; break;
            case 7: birthMonthAdd = 0; break;
            case 8: birthMonthAdd = 3; break;
            case 9: birthMonthAdd = 6; break;
            case 10: birthMonthAdd = 1; break;
            case 11: birthMonthAdd = 4; break;
            case 12: birthMonthAdd = 6; break;
          }
          if (birthYearRemainder==0) {
            leap = -1;
          } else {
            leap = 0;
          }
          total = ((birthMonthAdd + birthDay + (birthYear / 4) + birthYear + leap)%7);
          String day = null;
          switch(total) {
            case 0:
              day = "Saturday";
              break;
            case 1:
              day = "Sunday";
              break;
            case 2:
              day = "Monday";
              break;
            case 3:
              day = "Tuesday";
              break;
            case 4:
              day = "Wednesday";
              break;
            case 5:
              day = "Thursday";
              break;
            case 6:
              day = "Friday";
              break;
            default:
              System.err.println("An error occurred.");
              System.exit(0);
          }
          if(birthMonth > 12 || birthMonth < 0) {
            System.out.println("An error occurred.");
          } else if (birthYear > 101 || birthYear < 1) {
            System.out.println("An error occurred.");
          } else {
            System.out.println("You were born on a " +day);
          }
          System.out.println("Continue?");
          cont = !in.nextLine().equalsIgnoreCase("stop");
        }
      }
    }
    Thing is, teacher just wanted me to input three numbers like so:
    10 20 1990
    And when you hit enter it tells you the day you were born.
    Would this still be possible to do in a while statement?

  14. #12
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Case Switch Help?

    Yes, it would do that with the above code, except it would ask
    "Continue?"
    after all that. Run the code and see for yourself

  15. #13
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Case Switch Help?

    Am I placing the while in the beginning of the program?
    If I do it like that, it's giving me multiple errors.

  16. #14
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Case Switch Help?

    I edited the code that I posted, look at how I did it.

  17. #15
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Case Switch Help?

    I got it working now, but what do I type after Continue pops up?
    I'm inputting cont, yes, true, and each just ends the process.
    Well, it gives this:
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at BirthdayEdit.main(BirthdayEdit.java:14)

  18. #16
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Case Switch Help?

    Hmm thats interesting, one second

  19. The Following User Says Thank You to Tjstretch For This Useful Post:

    xionyus (October 19th, 2011)

  20. #17
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Case Switch Help?

    import java.util.Scanner;
     
    public class BirthdayEdit {
     
    	public static void main (String arg[])
    	{
    		Scanner sc = new Scanner(System.in);
    		boolean cont = true;
    		while(cont) {
    		int birthMonth = sc.nextInt();
    		int birthDay = sc.nextInt();
    		int birthYear = (sc.nextInt() - 1900);
    		double birthYearRemainder = birthYear % 4;
    		int leap;
    		int total;
    		int birthMonthAdd = 0;
    		switch (birthMonth) {
    			case 1: birthMonthAdd = 1; break;
    			case 2: birthMonthAdd = 4; break;
    			case 3: birthMonthAdd = 4; break;
    			case 4: birthMonthAdd = 0; break;
    			case 5: birthMonthAdd = 2; break;
    			case 6: birthMonthAdd = 5; break;
    			case 7: birthMonthAdd = 0; break;
    			case 8: birthMonthAdd = 3; break;
    			case 9: birthMonthAdd = 6; break;
    			case 10: birthMonthAdd = 1; break;
    			case 11: birthMonthAdd = 4; break;
    			case 12: birthMonthAdd = 6; break;
    		}
    		if (birthYearRemainder==0) {
    			leap = -1;
    		} else {
    			leap = 0;
    		}
    		total = ((birthMonthAdd + birthDay + (birthYear / 4) + birthYear + leap)%7);
    		String day = null;
    		switch(total) {
            case 0:
            day = "Saturday";
            break;
            case 1:
            day = "Sunday";
            break;
            case 2:
            day = "Monday";
            break;
            case 3:
            day = "Tuesday";
            break;
            case 4:
            day = "Wednesday";
            break;
            case 5:
            day = "Thursday";
            break;
            case 6:
            day = "Friday";
            break;
            default:
            System.err.println("An error occurred.");
            System.exit(0);
    		}
    		if(birthMonth > 12 || birthMonth < 0) {
            System.out.println("An error occurred.");
            } else if (birthYear > 101 || birthYear < 1) {
            System.out.println("An error occurred.");
            } else if (birthMonth == 2 && birthDay >= 30) {
            System.out.println("An error occurred.");
            } else {
            System.out.println("You were born on a " +day);
            }
            System.out.println("Continue?");
            cont = !sc.nextLine().equalsIgnoreCase("stop");
    		}
        }
    }

    That's my code at the moment.

  21. #18
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Case Switch Help?

    Ok I got it working with:
    import java.util.Scanner;
     
    public class BirthdayEdit {
     
      public static void main (String arg[])
      {
        Scanner sc = new Scanner(System.in);
        boolean cont = true;
        while(cont)
        {
          System.out.println("What is your birth month?");
          int birthMonth = sc.nextInt();
          System.out.println("What is your birth day?");
          int birthDay = sc.nextInt();
          System.out.println("What is your birth year?");
          int birthYear = (sc.nextInt() - 1900);
          double birthYearRemainder = birthYear % 4;
          int leap;
          int total;
          int birthMonthAdd = 0;
          switch (birthMonth) {
            case 1: birthMonthAdd = 1; break;
            case 2: birthMonthAdd = 4; break;
            case 3: birthMonthAdd = 4; break;
            case 4: birthMonthAdd = 0; break;
            case 5: birthMonthAdd = 2; break;
            case 6: birthMonthAdd = 5; break;
            case 7: birthMonthAdd = 0; break;
            case 8: birthMonthAdd = 3; break;
            case 9: birthMonthAdd = 6; break;
            case 10: birthMonthAdd = 1; break;
            case 11: birthMonthAdd = 4; break;
            case 12: birthMonthAdd = 6; break;
          }
          if (birthYearRemainder==0) {
            leap = -1;
          } else {
            leap = 0;
          }
          total = ((birthMonthAdd + birthDay + (birthYear / 4) + birthYear + leap)%7);
          String day = null;
          switch(total) {
            case 0:
              day = "Saturday";
              break;
            case 1:
              day = "Sunday";
              break;
            case 2:
              day = "Monday";
              break;
            case 3:
              day = "Tuesday";
              break;
            case 4:
              day = "Wednesday";
              break;
            case 5:
              day = "Thursday";
              break;
            case 6:
              day = "Friday";
              break;
            default:
              System.err.println("An error occurred.");
              System.exit(0);
          }
          if(birthMonth > 12 || birthMonth < 0) {
            System.out.println("An error occurred.");
          } else if (birthYear > 101 || birthYear < 1) {
            System.out.println("An error occurred.");
          } else if (birthMonth == 2 && birthDay >= 30) {
            System.out.println("An error occurred.");
          } else {
            System.out.println("You were born on a " +day);
          }
          System.out.println("Continue?");
          sc.nextLine();
          cont = !sc.nextLine().equalsIgnoreCase("no");
        }
      }
    }

    The problem seems to be that the sc.nextInt() was doing funky stuff again, type no to discontinue
    Last edited by Tjstretch; October 19th, 2011 at 09:01 PM.

Similar Threads

  1. orphaned case
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 08:35 PM
  2. Case statements to Method calls???
    By Java Neil in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2011, 06:17 AM
  3. Ranging Case Values in Switch Construct
    By WhiteSoup12 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 6th, 2011, 11:50 AM
  4. CompareToIngnore case problem
    By newTaz in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 7th, 2010, 05:28 PM
  5. [SOLVED] upper case
    By andaji in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2010, 11:54 PM