-7

Write a java code for converting the seconds to minutes, seconds, hours and days. But no value of 0 should be printed.

****For example :****

If the user gives 102, program should print 1 minutes 42 seconds .

NOT 1 minutes 42 seconds 0 hours 0 days.

But I have written a code which show '0' value. Like, If the user gives 102, the program print, 0 day(s) 0 hour(s) 1 minute(s) 42 second(s).

import java.util.Scanner;

public class clock {

  public static void main(String[] args) {
    Scanner keyboard=new Scanner(System.in);

    System.out.println("Please input seconds");
    int seconds = keyboard.nextInt();
    int minute,hour,day;

    minute = seconds/60;
    seconds = seconds%60;
    hour = minute / 60;
    minute = minute % 60;
    day = hour / 24;
    hour = hour % 24; 
    System.out.printf("%d seconds and %d minutes %d hours %d days",seconds,minute,hour,day);
  }
}

That's also fine even if someone can give me the algorithm :)

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99

3 Answers3

-2

You need to only print values if they are not 0. So you need to check if a value is zero. You do this using an if-statement like this:

if( day == 0) {
  // Code here will only execute if the value of day is zero
}

Or, if you only want to trigger if the value is not zero, you do a check that day != 0.

This way, we can build up a string that only has values that are not zero:

String output = "";

// First the days
if( days != 0 ) {
  output += days + " days";
}

// Then the hours
if( hours != 0 ) {
  output += hours + " hours ";
}

// etc

Then when you've finished, you can print the resulting line.

System.out.println(output.trim());

We call the trim() method to get rid of any extra whitespace on the beginning or end of the line.

Note that if you're using numbers, and the number can be negative, you might want to check that days > 0 instead of just not equal. Otherwise you might be printing -1 days which is weird.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
-2

Yahoo ! Get the answer ! PROBLEM IS SOLVED VERY EASILY !!!!

import java.util.Scanner;
public class clock {
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("please input the seconds");
    int seconds = keyboard.nextInt();
    int minutes, hour, day ;   
    minutes = seconds / 60; 
    seconds = seconds % 60; 

    hour = minutes / 60; 
    minutes = minutes % 60;

    day = hour / 24 ;
    hour = hour % 24 ; 

    if (seconds >0){
         System.out.printf("%d Seconds ",seconds);
    }
    if (minutes > 0){
         System.out.printf("%d Minutes ",minutes);
    }
    if (hour > 0){
         System.out.printf("%d Hour ",hour); 
    }
    if (day > 0){
    System.out.printf("%d day ",day);
    }
  }
}
-3

Make an edit to your code :

public static void main(String[] args) { Scanner keyboard=new Scanner(System.in);

  System.out.println("Please input seconds");
  int seconds = keyboard.nextInt();
  int minute,hour,day;

  minute = seconds/60;
  seconds = seconds%60;
  hour = minute / 60;
  minute = minute % 60;
  day = hour / 24;
  hour = hour % 24; 
  StringBuffer sb = new StringBuffer(); //This is used based on your requirement for dynamic output,each of the below conditions will add chars to string accordingly
  if(seconds != 0){
   sb.append(seconds +" seconds and ");}
  if(minute !=0){
   sb.append(minute +" minutes ");}
  if(hour != 0){
   sb.append(hour +" hours ");}
  if(day != 0){
    sb.append(day +" days");}
  System.out.printf(sb.toString());

}

  • You should provide a helpful explanation to supplement the code suggestion, i.e. what you changed and why it is needed – Tyler Oct 03 '17 at 17:40
  • Is there any way to code this without using String Buffer and String Builder Classes ????? – Shahriar Mim Oct 03 '17 at 18:09
  • you can use string concatenation , but string buffer uses less memory and faster in handling strings. – Nikhil kumar Mallela Oct 04 '17 at 18:57
  • That is actually [not the case](https://stackoverflow.com/a/46513041/419705) in Java 9 anymore, and code that went out of its way to use StringBuilders actually takes a performance hit now that Java has gotten smarter about how it handles string concatenation. :) – Roddy of the Frozen Peas Oct 05 '17 at 03:26