Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8881145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:17:12+00:00 2026-06-14T20:17:12+00:00

Hi Im still new to java and Im having trouble working out passing values

  • 0

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);
}
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:17:13+00:00Added an answer on June 14, 2026 at 8:17 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble working out why java can't see my mysql driver: I've downloaded
I am fairly new to java having come from a VS2010 background. Currently working
I'm having some trouble removing null values from a 2D array. I'm still a
So I'm transitioning from Java to C++ and am still having trouble trying to
Hey all, I'm still relatively new to Java, and looking for a sanity check.
i am still new to the Java language and libraries... i often use this
I'm still fairly new to Java, but I'm fairly sure this shouldn't happen. There's
I'm still new to programming/Java/Android so I'm trying to understand everything I do and
I'm still relatively new to Java and Android development, so I'm still unfamiliar with
I'm relatively new to Java and I'm still trying to understand the fundamentals. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.