1

i'm trying to write a program that can convert seconds into days, hours, minutes, seconds. But I am trying to make it loop so that once converted the program will prompt the user to enter another number of seconds, or if the number is negative end the program. So far, my problem seems to be with my while loop since the number of seconds isnt necessarily 0 it keeps trying to solve and prompt. Here is my code:

import java.util.*;
public class Probleme3 
{
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    System.out.print("Input seconds :");
    int sec = in.nextInt();
    int min;
    int heure;
    int jour;

    while(sec != 0)
    {
        if(sec < 0)
        {
        }

        else
        {
        min = sec /60;
        sec = sec % 60;
        heure = min /60;
        min = min % 60;
        jour = heure /24;
        heure = heure % 24;


        System.out.println( jour + ":" + heure + ":" + min + ":" + sec);

        System.out.println("Input seconds :");
    }
    }   
}
}
4
  • change it to a do while and put the scanner in the beginning of the loop Commented Jan 29, 2020 at 20:43
  • sec = in.nextInt(); this is the part that goes at the beginning Commented Jan 29, 2020 at 20:45
  • If you pass it, let's say, 58 seconds: 58 % 60 = 58 != 0 is true making this an infinite loop. Replace the while (sec != 0) for while (sec > 0), then all %` for -. Same example: 58 - 60 = -2 > 0 is false thus breaking the loop. Commented Jan 29, 2020 at 21:27
  • And this will always be an infinite loop if sec < 0. Commented Jan 29, 2020 at 21:52

4 Answers 4

1

remove the while loop:

import java.util.*;

public class Probleme3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input seconds :");
        int sec = in.nextInt();
        int min;
        int heure;
        int jour;

        min = sec / 60;
        sec = sec % 60;
        heure = min / 60;
        min = min % 60;
        jour = heure / 24;
        heure = heure % 24;

        System.out.println(jour + ":" + heure + ":" + min + ":" + sec);

        in.close();
    }
}

for multiple times, use this:


import java.util.*;

public class Probleme3 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int sec;
        int min;
        int heure;
        int jour;
        while(true) {
            System.out.print("Input seconds :");
            sec = in.nextInt();

            if(sec<0) {
                break;
            }

            min = sec / 60;
            sec = sec % 60;
            heure = min / 60;
            min = min % 60;
            jour = heure / 24;
            heure = heure % 24;

            System.out.println(jour + ":" + heure + ":" + min + ":" + sec);

        }
        System.out.println("Program exits.");
        in.close();
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Best and most optimal solution!
Not if you want to prompt the user to enter another number of seconds, or if the number is negative end the program...
@DenisDijak here it goes
@pkm well you kinda created indefinite loop :) try to enter -1 and see what happens
@pkm yes, but it shouldn't. If the number is negative end the program.
|
0

Well, first of all, if you want to sequentially work with new inputs, you will have to move the input prompts into the loop. Now the issue with your loop is, that you only set sec in the line sec = sec % 60 and then the loop stops if sec == 0. However this can only happen if is sec is devisable by 60. If I understand your question correctly, something like this should work:

import java.util.*;
public class Probleme3 
{
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    System.out.print("Input seconds :");
    int min = 0;
    int heure = 0;
    int jour = 0;

    while(sec != 0)
    {
        System.out.println("Input seconds: ");
        sec = in.nextInt();
        if(sec < 0)
        {
            System.out.println("No values below zero allowed!");
            continue;
        }
        else
        {
            min = sec /60;
            sec = sec % 60;
            heure = min /60;
            min = min % 60;
            jour = heure /24;
            heure = heure % 24;
        }

        System.out.println( jour + ":" + heure + ":" + min + ":" + sec);
    }   
}

1 Comment

This won't compile, sec in while loop that is redudant in this case is not initilized anywhere.
0

You are reading the user's input only once so the value of 'sec' never changes, since it never changes, it will create an infinite loop. This should do the work:

import java.util.*;
public class Probleme3 
{
    public static void main(String[] args)
    {

    int sec = -1; 

    while(sec != 0)
    {
      if(sec == 0)
      {
            return;
      }

      else
      {
            Scanner in = new Scanner(System.in);
            System.out.print("Input seconds :");
            sec = in.nextInt();
            int min;
            int heure;
            int jour;
            min = sec /60;
            sec = sec % 60;
            heure = min /60;
            min = min % 60;
            jour = heure /24;
            heure = heure % 24;

            System.out.println( jour + ":" + heure + ":" + min + ":" + sec);

            System.out.println("Input seconds :");
        }
    }   
    }
}

3 Comments

This won't work. It will throw you out of the loop as soon as you enter 60 seconds for example... sec = 60 % 60 = 0 and good bye loop
This solves the user's question (how to fix the infinite loop?), edge cases as 60 seconds are not part of the question and can be solved by the user.
You should look at the bigger picture not just give halfway answers
0

You can do something like this for more clean solution and use java Time API if your using java 8 or above.

public static void main(String... args)
{
    int sec;
    Duration dur;
    Scanner in = new Scanner(System.in);
    while(true)
    {
            System.out.print("Input seconds :");
            sec = in.nextInt();
            if (sec < 0)
                break;

            dur = Duration.of(sec, ChronoUnit.SECONDS);
            System.out.println( dur.toDaysPart() + ":" + dur.toHoursPart() + ":" + dur.toMinutesPart() + ":" + dur.toSecondsPart());
    }
    in.close();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.