4 Replies Latest reply: Nov 28, 2012 5:28 AM by 975630 RSS

    Having trouble passing values between methods

    975875
      So if this isnt the right spot for this.
      Anyhow any coments on where I went wrong or need to fix will be apriciated.
      So I got two methods that return a value (getHour() and getMinute(). The values that are returned I am tring to pass into showTime. However it doesn't seem to work as all I get is zero's.
      I've done a search around on the net and Im starting to thing my intellagnce is limited.
       
      
      import java.util.Scanner;
      
      public class First
      {
      
       static int sH, sM;
          public static void main(String args[]){
       
              
              getHour();
              getMinute();
             showTime(sH,sM);
          }
          
          static int getHour(){
              Scanner input = new Scanner(System.in);
              System.out.println("Please enter the hour: ");
              int setHour = input.nextInt();
              
      
              if(setHour <= 24){
                  System.out.println("You entered " +setHour+ " for the hour.");
              }else{
                  System.out.println("Please enter the hour number from 0 to 24");
                  getHour();
              }
           return sH;
          }
          
           static int getMinute(){
              Scanner input = new Scanner(System.in);
              System.out.println("Please enter the minutes: ");
              int setMinute = input.nextInt();
      
              if(setMinute <= 60){
                  System.out.println("You entered " +setMinute+ " for the minutes.");
              }else{
                  System.out.println("Please enter the hour number from 0 to 60");
                  getMinute();
              }
              return sM;
          }
          
          private static void showTime(int sH, int sM){
            
           
                System.out.println(+sH+":"+sM);
          }
      }
        • 1. Re: Having trouble passing values between methods
          Kayaman
          Well, first of all you're returning a value in both your get methods, yet you're not actually doing anything with the return values.
          Second of all, in your methods you're returning the values of sH and sM, although they haven't been assigned values.

          You should be returning setHour and setMinute instead of sH and sM, and assigning them to your sH and sM variables.
          • 2. Re: Having trouble passing values between methods
            TPD-Opitz
            Welcome to the forum.

            To avoid this in future you should do both: don't declare member variables unless you have a good reason and find yourself a naming convention that undoubtable destinguishes member variables from local variables.
            For the latter you should at least adjust the coloring model of your IDE to make member and local variables look differently.

            bye
            TPD
            • 3. Re: Having trouble passing values between methods
              aksarben
              Advice: Invest a few hours in learning to use your IDE's debugger. With a debugger, it would be immediately obvious that no value is ever stored in the two static variables.

              Time spent learning to use a debugger will be saved many times over as you work on additional assignments/projects/applications.
              • 4. Re: Having trouble passing values between methods
                975630
                Hi,pls compare ur coding with the one i have posted below. In your code, the variables sH and sM are still in initialised state(value =0) and its not assigned with the values returned by getHour and getMinute methods. This is why the shoTime method returned zero. Hope i helped you.

                import java.util.Scanner;

                /**
                *
                * @author Jaison KS-IT
                */
                public class First {
                static int sH, sM;
                public static void main(String args[])
                {

                getHour();
                getMinute();
                showTime(sH,sM);
                }

                static int getHour(){
                Scanner input = new Scanner(System.in);
                System.out.println("Please enter the hour: ");
                int setHour = input.nextInt();

                if(setHour <= 24){
                System.out.println("You entered " setHour " for the hour.");
                sH = setHour;
                }else{
                System.out.println("Please enter the hour number from 0 to 24");
                getHour();
                }
                return sH;
                }
                static int getMinute(){
                Scanner input = new Scanner(System.in);
                System.out.println("Please enter the minutes: ");
                int setMinute = input.nextInt();

                if(setMinute <= 60){
                System.out.println("You entered " setMinute " for the minutes.");
                sM = setMinute;
                }else{
                System.out.println("Please enter the hour number from 0 to 60");
                getMinute();
                }
                return sM;
                }
                private static void showTime(int sH, int sM){

                     
                          System.out.println(+sH+":"+sM);
                }
                }