I'm having some trouble getting the times to show up correctly. inputTime is running fine, but neither displayStandardTime nore displayMilitaryTime is displaying. I've been playing with this for a couple of hours and can't figure it out. I'm probably missing something small, but it's driving me nuts in a big way.

//TimeMain.java

public class TimeMain
{
    public static void main(String[] args)
    {
        Time theTime = new Time();

        theTime.inputTime(); //ask user for input
        theTime.displayStandardTime();  //show standard time
        theTime.displayMilitaryTime();  //show military time
    }
}
//Time.Java

import java.util.Scanner;

public class Time
{
    private int hour;
    private int minute;
    private int second;

    /*public TimeMain()
    {

    }*/

    public void inputTime()
    {
        Scanner input = new Scanner(System.in);

        //hour input and qualifiers
        System.out.print("Enter the hour(s): "); //prompt user for hours
        hour = input.nextInt(); //read hour from user
        if (hour > 12 || hour < 1)
        {
            System.out.print("Please enter a value from 1 to 12.");
        }

        //minute input and qualifiers
        System.out.print("Enter the minute(s): "); //prompt user for minutes
        minute = input.nextInt(); //read minute from user
        if (minute > 60 || minute < 0)
        {
            System.out.print("Please enter a value from 0 to 60.");
        }

        //second input and qualifiers
        System.out.print("Enter the second(s): "); //prompt user for seconds
        second = input.nextInt(); //read second from user
        if (second > 60 || second < 0)
        {
            System.out.print("Please enter a value from 0 to 60.");
        }
    }

    public String displayStandardTime()
    {
        return "Standard Time: " + hour + ":" + "minute" + ":" + "second";
    }

    public String displayMilitaryTime()
    {
        int mhour = hour + 12;
        return "Military Time: " + mhour + ":" + "minute" + ":" + "second";
    }
}

Recommended Answers

All 3 Replies

You have no System.out.println statements in either the functions themselves or the calling statements. You create the String you want to display to the screen, but never display the String. Since your functions have the word "Display" in them, I imagine you want the System.out.println command in the functions. And do you really need/want to return anything from the functions?

public void displayStandardTime()
    {
        System.out.println ("Standard Time: " + hour + ":" + "minute" + ":" + "second");
    }
commented: Good insight and very helpful. Thanks! +4

What Vernon Said would work, but if you need your methods to return String from some reason; there is an alternative.

As he also mentioned, you could also do...
Instead of changing your methods, just keep your methods the same and instead of writing:

theTime.displayStandardTime();
theTime.displayMilitaryTime();

Change them to:

System.out.println(theTime.displayStandardTime());
System.out.println(theTime.displayMilitaryTime());

Hope this helps =).

commented: good insight and very helpful +4

Thanks for your help. It's working great now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.