CAn anyone help me wit the?? scanner stdin?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shadachi
    New Member
    • May 2007
    • 15

    #16
    Originally posted by r035198x
    You haven't explained how you are going to get the other days except for February.
    Are you planning on using lots of if-else statements like?

    [CODE=java]if(month.equals ("Jan")) {
    days = 31;
    }
    else ....[/CODE]
    That would solve the problem but wouldn't be neat.
    An obvious replacement for bunch of if-else is the switch ...
    using the switch... i see .. but i dono how to merge them all .. from the 1st example i posted ..using the switch and the 2nd using the arrays.. how to merge them ...

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #17
      Also since the input is given as an integer, I don't see why you need the array of months as Strings.

      Comment

      • shadachi
        New Member
        • May 2007
        • 15

        #18
        So ..the better solution is by using the switch??

        but nomad suggested I would make a array or an array list which would hold your months and days and then use an if statement using a scanner for the input

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #19
          Originally posted by shadachi
          using the switch... i see .. but i dono how to merge them all .. from the 1st example i posted ..using the switch and the 2nd using the arrays.. how to merge them ...
          It's not a matter of having to merge them. It's coming up with a simple algorithm. Something like when months entered is in (1, 3, 5, 7, 8,10, 12) then days in month is 31. A switch is well suited for this.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #20
            Originally posted by shadachi
            So ..the better solution is by using the switch??

            but nomad suggested I would make a array or an array list which would hold your months and days and then use an if statement using a scanner for the input
            There are lots of solutions as you have already heard in this thread. Write them all if you have time and compare them yourself.

            Comment

            • shadachi
              New Member
              • May 2007
              • 15

              #21
              Originally posted by r035198x
              It's not a matter of having to merge them. It's coming up with a simple algorithm. Something like when months entered is in (1, 3, 5, 7, 8,10, 12) then days in month is 31. A switch is well suited for this.
              ERm ..i get the idea d.. but the thing is i don't know much from switch.. i learned not much..can try and put an example..so i can learn? .sorry to bother u so much ><

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #22
                Originally posted by shadachi
                ERm ..i get the idea d.. but the thing is i don't know much from switch.. i learned not much..can try and put an example..so i can learn? .sorry to bother u so much ><
                I'm afraid if I give you the link to the tutorial for it, I'll have spoilt it for you and given you the answer ....

                Don't have some text book that describes it? Even in c+, the concept/syntax is the same?

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #23
                  Originally posted by r035198x
                  I'm afraid if I give you the link to the tutorial for it, I'll have spoilt it for you and given you the answer ....

                  Don't have some text book that describes it? Even in c+, the concept/syntax is the same?
                  Here's one that looks safe enough.

                  Comment

                  • shadachi
                    New Member
                    • May 2007
                    • 15

                    #24
                    Originally posted by r035198x
                    Here's one that looks safe enough.

                    in the switch statement rite?

                    i put
                    Code:
                    switch(month){
                    
                    case1:
                    case3;
                    case5;
                    numdays=31;
                    break;
                    
                    default:
                                    
                    System.out.println("Invalid month.");
                                    break;
                    }
                    
                    System.out.println("The Number of Days in " +month+"," +year+ " is "+numDays);
                    the output i got. is the number of days in 1,2006 is 31.

                    how to make the month instead of numbers become words like if u input 1 becomes january. 2 =february

                    Comment

                    • blazedaces
                      Contributor
                      • May 2007
                      • 284

                      #25
                      Originally posted by shadachi
                      in the switch statement rite?

                      i put
                      Code:
                      switch(month){
                      
                      case1:
                      case3;
                      case5;
                      numdays=31;
                      break;
                      
                      default:
                                      
                      System.out.println("Invalid month.");
                                      break;
                      }
                      
                      System.out.println("The Number of Days in " +month+"," +year+ " is "+numDays);
                      the output i got. is the number of days in 1,2006 is 31.

                      how to make the month instead of numbers become words like if u input 1 becomes january. 2 =february
                      Why don't you store months in an array or arraylist? Then you would do months[index] and it would call the correct month?

                      Comment

                      • shadachi
                        New Member
                        • May 2007
                        • 15

                        #26
                        Originally posted by blazedaces
                        Why don't you store months in an array or arraylist? Then you would do months[index] and it would call the correct month?

                        how do u make them into arrays.. coz i am using switch statements.. can u giv an example?

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #27
                          Originally posted by shadachi
                          how do u make them into arrays.. coz i am using switch statements.. can u giv an example?
                          If you have declared an array say

                          Code:
                          String[] months = {"January", "February", ...};
                          Then if the user inputs 1 and you store it into an int variable called month, then you can do
                          [CODE=java]System.out.prin tln("Month entered is" + months[month -1 ]);[/CODE]

                          Do you understand that part?

                          Comment

                          • shadachi
                            New Member
                            • May 2007
                            • 15

                            #28
                            not quite but the declaring part i do.. the 2nd part not so clear.. coz i din learn anything bout arrays yet

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #29
                              Originally posted by shadachi
                              not quite but the declaring part i do.. the 2nd part not so clear.. coz i din learn anything bout arrays yet
                              Well then you'll just have to learn them. Your initial problem description did not require you to express the month entered as a string so the switch stll solves the problem. If you want to show the name of the month entered then either use a dirty chunk of if-else statements or use the array.

                              Comment

                              • JosAH
                                Recognized Expert MVP
                                • Mar 2007
                                • 11453

                                #30
                                Originally posted by r035198x
                                Well then you'll just have to learn them. Your initial problem description did not require you to express the month entered as a string so the switch stll solves the problem. If you want to show the name of the month entered then either use a dirty chunk of if-else statements or use the array.
                                ... or a GregorianCalend ar object ...

                                kind regards,

                                Jos

                                Comment

                                Working...