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 7 of 7

Thread: Why is my output not right?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why is my output not right?

    Hello,

    I am fairly new to java and am creating a code that finds the number of days in a month. I figured it all out but, whenever I run my code it looks something like this:

    Enter a month: 2
    Enter a year: 2000
    February 2002 has 29 days

    I don't know why it is adding 2 to the year. It seems to be adding the number in the if (month == 2) to the year.

    Any help would be appreciated,
    Thanks ^^

    import java.util.Scanner;
     
    public class Exercise03_11 {
     
        public static void main(String args[]) {
            Scanner input = new Scanner(System.in);
     
            System.out.print("Enter a month: ");
            int month = input.nextInt();
            System.out.print("Enter a year: ");
            int year = input.nextInt();
            int days = 0;
     
            boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
     
            if (month == 1) {
                System.out.print("January ");
                days = 31;
            } else if (month == 2) {
                System.out.print("February ");
                if (isLeapYear) {
                    days = 29;
                } else {
                    days = 28;
                }
            } else if (month == 3) {
                System.out.print("March ");
                days = 31;
            } else if (month == 4) {
                System.out.print("April ");
                days = 30;
            } else if (month == 5) {
                System.out.print("May ");
                days = 31;
            } else if (month == 6) {
                System.out.print("June ");
                days = 30;
            } else if (month == 7) {
                System.out.print("July ");
                days = 31;
            } else if (month == 8) {
                System.out.print("August ");
                days = 31;
            } else if (month == 9) {
                System.out.print("September ");
                days = 30;
            } else if (month == 10) {
                System.out.print("October ");
                days = 31;
            } else if (month == 11) {
                System.out.print("November ");
                days = 30;
            } else if (month == 12) {
                System.out.print("December ");
                days = 31;
            } else {
                System.exit(0);
            }
            System.out.println(month + year + " has " + days + " days");
        }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Why is my output not right?

    System.out.println(month + year + " has " + days + " days");

    month + year is performing an arithmetic operation.
    Just print the year out instead of month, as you're already printing the month in the if-else block.

    Edit: Removed 'Switch' comment, not sure why I said switch instead of if-else
    Last edited by newbie; September 15th, 2011 at 08:00 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Bryan229 (September 15th, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why is my output not right?

    Thank you for the quick response

    Its for an assignment, the output has to include month and year. I already tried doing:
    month + " " + year
    But when I do that the output is:

    Enter a month: 2
    Enter a year: 2000
    February 2 2000 has 29 days

  5. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why is my output not right?

    Nevermind, I see what you mean, thanks!

  6. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why is my output not right?

    import java.util.Scanner;
     
    public class Exercise03_11 {
     
        public static void main(String args[]) {
     
    	int days = 31;
    	Scanner input = new Scanner(System.in);
     
    	String[] monthArray = {"January ", "February ", "March ", "April ", "May ", "June ",
    		           "July ", "August ","September ", "October ", "November ", "December "}; 
     
            System.out.print("Enter a month: ");
            int month = input.nextInt();
            System.out.print("Enter a year: ");
            int year = input.nextInt();
            boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
     
        	if (month == 4 || month == 6 || month == 9 || month == 11) days = 30;
    	if (month == 2) days = 28;
    	if (month == 2 && isLeapYear) days = 29;
    	if (month < 1 || month > 12) System.exit(0);
     
    	System.out.println(monthArray[month - 1] + Integer.toString(year) + " has " + days + " days");
        }	
    }

    fix'd

    A case would have worked well in this situation, but still too cumbersome for such a program

    OUTPUT:

    Enter a month: 2
    Enter a year: 2000
    February 2000 has 29 days
    Last edited by Leiferson; September 16th, 2011 at 02:12 AM. Reason: Forgot to add OUTPUT

  7. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Why is my output not right?

    Leiferson, although the OP had solved the issue, spoon-feeding is something that doesn't benefit the community, due to how it strips the recipient of the learning process.
    Suggested Reading: http://www.javaprogrammingforums.com...n-feeding.html
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  8. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Why is my output not right?

    I see at the bottom of your month number validation if..else:
    else {
                System.exit(0);
    From API docs for System.exit(int):
    The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
    You exit with a zero value to indicate "success", "normal", "we're good!" etc. A month value *not* in the range 1-12 is not good.

    Ideally you'd also explicitly say why the program has terminated - an *exceptional* outcome. Magic exit values are so last millennium. A Java-style way of doing this might be like this:
    public class Exercise03_11
    { // place brackets according to local convention. I've moved this one because I'm anally retentive
      // a final static array of months gives you instant integer-to-month-name goodness
      private final static String[] MONTH_NAMES = {"January", "February", /* 9 more */ "December"};
      public static String getMonth(int monthNumber)
      {
        // the month-number-to-array-index shift is an *implementation detail* which is why we hide it inside this method
        return MONTH_NAMES[monthNumber - 1];
      }
      ...
      public static void main(String[] args)
      {
        ...
            int month = input.nextInt();
        System.out.println("Month number " + month + " is " + getMonth(month));
      }

    ... so you could replace your massive if..else with just a couple of lines *and* expose it as a public static method, so that the next time you're writing exercise code and need a month-number-to-name conversion, you can just code:
      System.out.println("the month is " + Exercise03_11.getMonth(myMonthNumber));

    ... but what's really good about doing it this way is that you no longer need your System.exit(int) because Java will throw an Exception with useful information in it if the user types something other than 1-to-12 for the month. Try it out and see what happens.

    I don't think it would be a good idea to copy-and-paste this into your homework: it'll be very obviously not your code. I just thought I'd throw it in so you could compare it with your own code for how someone else might code this in Java and what other features of Java can reduce the amount of typing you have to do *and* make your code more reliable and useful.

Similar Threads

  1. [SOLVED] Can't get output HELP
    By moodycrab3 in forum Object Oriented Programming
    Replies: 8
    Last Post: February 7th, 2011, 03:07 AM
  2. Output of 0 or 100?
    By Scotty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 2nd, 2011, 05:15 PM
  3. Why am I getting this output?
    By ptabatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 6th, 2010, 07:36 PM
  4. need help with output.
    By VictorCampudoni in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 24th, 2010, 11:25 PM
  5. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM