My issue is with the "if" statement. Basically I want the program to have proper grammar when it states, "1 hour, 1 minute, 1 second", instead of "1 hours, 1 minutes, 1 seconds".

See what I mean? I gave it a go with "hours" but with no success.

Plus any advice as to how I could've made this program simpler. Keep in mind my knowledge of Java is limited. I'm currently learning it. So please don't bring up advanced methods of making it simpler. I'm limited to a broad knowledge of conditionals and loops (which I vaguely understand).

Thank you kindly!

//This program will convert the user's input of seconds into a combination of hours, minutes, and seconds

import java.util.Scanner;
public class Project7 
	{
	public static void main (String[]args)
		{
		int hrs,minutes,seconds,total;
		String time;
		Scanner scan = new Scanner (System.in);
		
		System.out.println("Please enter the amount of seconds to be converted ");
		total = scan.nextInt();
		hrs = total / 3600;
		minutes =  (total % 3600) / 60;
		seconds = (total % 3600) % 60;
		
		if (hrs < 2)
			time = hour;
		else
			time = hours;
			
		
		System.out.print(total + " seconds is " + hrs + time + minutes + " minutes and " + seconds + " seconds");
		}
	}

You want to add an "s" if the number of units is > 1?
Look at the tertiary operator. It is a quick way to do something like that in a single statement

String lbl = "hour" + ((hours > 1) ? "s" : "");

You want to add an "s" if the number of units is > 1?
Look at the tertiary operator. It is a quick way to do something like that in a single statement

String lbl = "hour" + ((hours > 1) ? "s" : "");

I'm still kinda lost, but I figured it out. Never used the tertiary operator before. Is there a more basic way of doing this. This is the result of my code and it works beautifully!

//This program will convert the user's input of seconds into a combination of hours, minutes, and seconds

import java.util.Scanner;
public class Project7 
	{
	public static void main (String[]args)
		{
		int hrs,min,sec,total;
		Scanner scan = new Scanner (System.in);
		
		System.out.println("Please enter the amount of seconds to be converted ");
		total = scan.nextInt();
		hrs = total / 3600;
		min =  (total % 3600) / 60;
		sec = (total % 3600) % 60;

		String lbl1 = "hour" + ((hrs > 1) ? "s" : " ");
		String lbl2 = "minute" + ((min > 1) ? "s" : " ");
		String lbl3 = "second" + ((sec > 1) ? "s" : " ");
			
		
		System.out.print(total + " seconds is " + hrs + " " + lbl1 + min + " " + lbl2 + "and " + sec + " " + lbl3);
		}
	}

Is there a more basic way

I doubt it.

Member Avatar for hfx642

A more basic way, is with a bunch of "if"s.
NormR1's way is elegant.

You might try building your output string as a separate string BEFORE printing it out.

String My_Output;
My_Output = "blah";
My_Output = My_Output + " blah";
My_Output = My_Output + " blah";
System.out.println (My_Output);
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.