Days in a month

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #16
    Originally posted by nomad
    Also I see another problem here

    Code:
     
    isLeapYear = consolein.nextBoolean();
     
    				if (isLeapYear) {
    					intdays = 29;
    				}  else {
    					intdays = 28;
    				}
    There is no test going how if some type in feb what will happen.

    nomad
    Didn't you just post this?

    EDIT: Nomad keeps editing his posts so now this one doesn't make any sense. Disregard it.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #17
      I agree with dmjpro (see reply#2): why not instantiate a GregorianCalend ar
      given any day in a month and a given year. The getActualMaximu m() gives
      you the number of days in that month in that year.

      kind regards,

      Jos

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #18
        Originally posted by JosAH
        I agree with dmjpro (see reply#2): why not instantiate a GregorianCalend ar
        given any day in a month and a given year. The getActualMaximu m() gives
        you the number of days in that month in that year.

        kind regards,

        Jos
        Jos you silly, silly boy. Using a GregorianCalend ar would be too easy. Besides what if he wants to eventually accept input from the user in sidereal time?

        Yes kids, its called sarcasm.

        Comment

        • nomad
          Recognized Expert Contributor
          • Mar 2007
          • 664

          #19
          Originally posted by RedSon
          Didn't you just post this?

          EDIT: Nomad keeps editing his posts so now this one doesn't make any sense. Disregard it.
          Sorry about that one RedSon.
          I read and I saw a mistake... and I wanted to correct it. You're to fast on the draw today.

          nomad

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #20
            Originally posted by RedSon
            Jos you silly, silly boy. Using a GregorianCalend ar would be too easy. Besides what if he wants to eventually accept input from the user in sidereal time?

            Yes kids, its called sarcasm.
            I have just one grown-up answer to that: :-P <kick-dust> So there! Duh!

            kind regards,

            Jos ;-)

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #21
              Originally posted by nomad
              Sorry about that one RedSon.
              I read and I saw a mistake... and I wanted to correct it. You're to fast on the draw today.

              nomad
              Huh, my wife complains about that too.

              Okay no more hijacking threads after this one.

              Comment

              • rents
                New Member
                • Mar 2007
                • 26

                #22
                Originally posted by RedSon
                There are quite a few problems with this but what chiefly catches my eye is:
                [CODE=java]
                System.out.prin tln ("The number of days in" + intmonth + "/" + intyear +" is:");[/CODE]

                Do you see anything wrong with this?

                is it too many +'s or the fact that I'm using intmonth? intyear? should it just be 'month' 'year'?

                Comment

                • RedSon
                  Recognized Expert Expert
                  • Jan 2007
                  • 4980

                  #23
                  Originally posted by rents
                  is it too many +'s or the fact that I'm using intmonth? intyear? should it just be 'month' 'year'?
                  its not about intmonth or intyear, although those are probably poor choices for variable names. It has to do with what comes after the ":".

                  Comment

                  • nomad
                    Recognized Expert Contributor
                    • Mar 2007
                    • 664

                    #24
                    Originally posted by rents
                    is it too many +'s or the fact that I'm using intmonth? intyear? should it just be 'month' 'year'?
                    You just want to print out the amount of days in a giving month right!!!!
                    SOooo!!!
                    nomad

                    Comment

                    • rents
                      New Member
                      • Mar 2007
                      • 26

                      #25
                      ahh crap..duhhhh. I am really dumb with this stuff.
                      I'm a complete novice and I think I make a bigger deal than I should out of this.

                      Comment

                      • rents
                        New Member
                        • Mar 2007
                        • 26

                        #26
                        ok, here is what I have so far. When I run the program, it asks you to enter the year and the month, and after that, nothing happens. What am I missing here in order to figure out the days? I can't think of what to do..

                        /*
                        * Main.java
                        *
                        * Created on May 26, 2007, 8:29 AM
                        *
                        *
                        *
                        *

                        */




                        package daysinamonthcal culator;
                        import java.util.Scann er;
                        /**
                        *
                        *
                        */
                        public class Main {

                        /** Creates a new instance of Main */
                        public Main() {
                        }

                        /**
                        * @param args the command line arguments
                        */
                        public static void main(String[] args) {
                        //Declare Variables
                        int intyear = 1;
                        int intmonth = 1;
                        int intdays = 1;
                        boolean isLeapYear;
                        Scanner consolein = new Scanner(System. in);




                        //Request User Input
                        System.out.prin tf ("\n\n Enter the year ");
                        intyear = consolein.nextI nt();
                        System.out.prin tf ("\n\n Enter the month ");
                        intmonth = consolein.nextI nt();


                        switch (intmonth) {
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12:
                        intdays = 31;
                        break;
                        case 4:
                        case 6:
                        case 9:
                        case 11:
                        intdays = 30;
                        break;

                        case 2:
                        //Check for leap year

                        System.out.prin tln ("Checking for leap year (true/false)");
                        isLeapYear = consolein.nextB oolean();

                        isLeapYear =
                        ((intyear % 4 == 0 && intyear % 100 != 0) || (intyear % 400 == 0));


                        if (isLeapYear) {
                        intdays = 29;
                        } else {
                        intdays = 28; }


                        //Display results
                        System.out.prin tln ("The number of days in" + intmonth + "/" + intyear +" is:" + intdays + "");
                        System.out.prin t ("Thank you. Run Completed");



                        }

                        }
                        }

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #27
                          Erm, <raises hand/>, hint: it starts with a capital G and ends with "regorianCalend ar".

                          kind regards,

                          Jos ;-)

                          Comment

                          • RedSon
                            Recognized Expert Expert
                            • Jan 2007
                            • 4980

                            #28
                            i think you should double check the position of your brackets

                            and you need to use code tags or i'm going to start getting cranky!

                            Comment

                            • emekadavid
                              New Member
                              • Mar 2007
                              • 46

                              #29
                              You were doing just fine until switch had to check for leap year. You seem nervous there. in life as well as in coding java, remember, KISS. Why did you have to check for nextBoolean when the user doesn’t know if it’s a leap year or not? Doesn’t make sense. That’s the work of your program. Take away consolein.nextB oolean from the logic of your code. After that line, you need to realize that you’re checking intyear (according to convention it should be intYear, pls!) for leap year where switch evaluates to 2. get it. So, what if we want to check for leap year? Keeping it simple, I’ve prepared something based on the principles below.
                              Code:
                              
                              public class Checker {
                              	public static void main(String[] args) {
                              		int intyear = 1996;  //assuming this is what the user entered.
                              		int intdays;  //you have this already from the user
                              		
                              		//the below if from your logic for checking leap years. don't have
                              		//the time to consult the wikipedia. 
                              		<Line removed to comply with homework posting guidelines>
                              		
                              		System.out.println("The number of days if february is chosen for "+
                              				intyear+" is "+intdays);
                              	}
                              }
                              //this code is based on principle i.e where your switch evaluates to 2.
                              now your leap year is resolved I believe.
                              Last edited by RedSon; May 29 '07, 02:48 PM. Reason: Removing line that spoon feeds homework solutions.

                              Comment

                              • JosAH
                                Recognized Expert MVP
                                • Mar 2007
                                • 11453

                                #30
                                <desparately trembling voice>
                                GregorianCalend ar!for!Pete's!s ake!
                                Are!you!all!dea f!or!something? !
                                <desparately trembling voice/>

                                kind regards,

                                Jos ;-)

                                Comment

                                Working...