Filex Amun
Filex Amun

Reputation: 3

Issue passing values into methods

Hi Im still new to java and Im having trouble working out passing values between methods. I still carnt get it right with all the researh I've done. Anyhow any comments or advice will be welcome. To save you reading though the code bellow I have two methods that return a value and I the pass them into showTime(). however with the current format I only get zero's. Any advice apart from give up now.

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 mintues: ");
    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);
}
 }

Upvotes: 0

Views: 110

Answers (6)

Phil K
Phil K

Reputation: 5359

You have the static variables sH and sM which are being passed into showTime to print the time, but they are never assigned a value. In fact you do not even need them at all.

Instead you should have your getHour function return the setHour variable, and your getMinute method return the setMinute variable.

Then you can simply call the showTime method like so:

public static void main(String args[]) {
    int hour = getHour();
    int minute = getMinute();
    showTime(hour, minute);
}

There is also an issue within your getHour and getMinute methods. They are recursively calling themselves when an invalid input is entered, yet the recursive method call isn't being returned. Thus the program logic will simply continue once the recursion is finished and return the original, invalid data. This can be solved by changing:

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();
}

to:

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");
  return getHour();
}

Obviously also do this for your getMinute method too.

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

You are not saving your values in sH or sM in your methods, and you are returning them. So, the values returned will be 0 only.

int setHour = input.nextInt();

this should be: -

sH = input.nextInt();

Also, you actually don't need to return them, if you have declared them as static variables outside your main method.


A better idea is not to use static variables. Declare your sH and sM variables inside your main method, as local variables.

Now when you return the values from getHour(), you can assign them to the local variables. And then work with them.

So, your main method can be modified to: -

public static void main(String args[]) {
    int sH = getHour();
    int sM = getMinute();
    showTime(sH, sM);
}

And in getHour(), change your return sH; to return setHour;. Similarly in getMinute() method.

And in the else part, change getHour() invocation to return getHour();

Upvotes: 1

nb6kr
nb6kr

Reputation: 11

In your getMinute() and getHour() methods you need to return the value where you store the users input. Also in this instance there's no need for static int sH and sM as everything is being handled by the same event, unless you wanted to access them later from a different event.

public static void main(String args[]){

//Store the returnb value of getHour in sH
int sH = getHour();

//Store the returnb value of getMinute() in sM
int sM = getMinute();

//Display the stored values
showTime(sH,sM);
}

private 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.");

    //return the users input
    return setHour;
}else{
    System.out.println("Please enter the hour number from 0 to 24");
    getHour();
}

return 0;

}

private int getMinute(){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the mintues: ");
int setMinute = input.nextInt();

if(setMinute <= 60){
    System.out.println("You entered " +setMinute+ " for the minutes.");

    //return the users input
    return setMinute;
}else{
    System.out.println("Please enter the hour number from 0 to 60");
    getMinute();
}

    return 0;

}

private static void showTime(int sH, int sM){
System.out.println(+sH+":"+sM);
}
}

Upvotes: 1

Mitra
Mitra

Reputation: 154

there are quite a few mistakes in your code the following code has to be replaced

public class First
{

static int sH, sM;
public static void main(String args[]){


First.sH = getHour();

First.sM = getMinute();
showTime(abc.sH,abc.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.");
    return 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 mintues: ");
int setMinute = input.nextInt();

if(setMinute <= 60){
    System.out.println("You entered " +setMinute+ " for the minutes.");
    return 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(First.sH+":"+First.sM);
}
 }

Your return sh and return sm were returning nothing which should be replaced by return setHour and return setMinute

Upvotes: 0

Shahrukh A.
Shahrukh A.

Reputation: 1101

You didn't assigne values in methods to your static properties.

 static int sH, sM;

so thats why you are getting 0.

sH = input.nextInt(); sM = input.nextInt();

Upvotes: 0

akaIDIOT
akaIDIOT

Reputation: 9231

Your variables sH and sM are never assigned a value, you're just returning them unchanged. Try returning setHour and setMinute instead, passing those to showTime.

Upvotes: 0

Related Questions