-3

I am trying to make a time calculator. (Made a post about it earlier, and I fixed it). The code is working as intended, except when I enter the number 500,000. It just automatically terminates the program with no print statement. However I can enter anything from 1 - 488888 and 544444 - 900000 if I wished and it would work.

I've moved my if-statements around, nested a few here and there. I can't gauge where the problem rises in the if/else if statements.


final int x = 9;
final int y = 1;
final int Days;
final int Hours;
final int Minutes;
final int Seconds;

Days = total_seconds / 86400;
Hours = (total_seconds % 86400 ) / 3600;
Minutes = ((total_seconds % 86400 ) % 3600 ) / 60;
Seconds = ((total_seconds % 86400 ) % 3600 ) % 60;

if (Hours == 0) {

    if (Minutes < x || Seconds < x) {
    String padded = String.format("%02d" , Minutes);
    String padded2 = String.format("%02d" , Seconds);

    System.out.print("You entered " + total_seconds + " second(s), which is " + Minutes + " minute(s), and " +  Seconds + " second(s).");
    System.out.print("\n");
    System.out.print(padded + ":" + padded2);
    }
}
else if (Hours >= y && Days == 0) {
    if (Minutes < x || Seconds < x) {
    String padded = String.format("%02d" , Minutes);
    String padded2 = String.format("%02d" , Seconds);

    System.out.print("You entered " + total_seconds + " second(s), which is " + Hours + " hour(s), " + Minutes + " minute(s), and " +  Seconds + " second(s).");
    System.out.print("\n");
    System.out.print(Hours + ":" + padded + ":" + padded2 + " hours.");
    }
}

else if (Days >= y && Hours >= y) {
    if (Minutes < x || Seconds < x) {
    String padded = String.format("%02d" , Minutes);
    String padded2 = String.format("%02d" , Seconds);

    System.out.print("You entered " + total_seconds + " second(s), which is " + 
    Days + " day(s), " + Hours + " hour(s), " + padded + " minute(s), and " + 
    padded2 + " second(s).");
    System.out.print("\n");
    System.out.print(Days + " day(s) " + Hours + ":" + padded + ":" + padded2 + 
    " hour(s).");
    }
}

else {

System.out.print("You entered " + total_seconds + " second(s), which is " + Days + " day(s), " + Hours + " hour(s), " + Minutes + " minute(s), and " + Seconds + " second(s).");
System.out.print("\n");
System.out.print(Days + " day(s) " + Hours + ":" + Minutes + ":" + Seconds + " hour(s).");
}

When I enter any number that has a number of Days, it should output:

"You entered 500,000 seconds, which is 5 days, 18 hours, 53 minutes, and 20 seconds. (5 days 18:53:20 hours)"

The actual output (when I input 500,000) is absolutely nothing. The program just auto terminates with no print.

I have added else, but problem persists.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 1
    You describe 1 input, presumably `total_seconds`, but you haven't shared the code that initialized all your other variables: `Days`, `Hours`, `Minutes`, `Seconds` – charles-allen Oct 28 '19 at 03:41
  • 4
    What is the exact value of all the variables in `Days >= y && Hours >= y` and `Minutes < x || Seconds < x` during the test case that it's giving the wrong output? – Carcigenicate Oct 28 '19 at 03:41
  • alright, edited – Aria Sentori Oct 28 '19 at 03:45
  • 1
    As you said "5 days, 18 hours, 53 minutes and 20 seconds", so let's follow the code: `if (Hours == 0)` Fail, so: `else if (Hours >= y && Days == 0)` Fail, so: `else if (Days >= y && Hours >= y)` OK, so: `if (Minutes < x || Seconds < x)` Fail (both minutes and seconds are >=9), so: **No else clause, so nothing printed!** --- Now read: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Oct 28 '19 at 04:03
  • You dont need "padded variables"... Remove `if (Minutes < x || Seconds < x)` and `String.format("%02d" , 10)` will still show 10 – OneCricketeer Oct 28 '19 at 04:05
  • To Andreas: Edited. Problem persists. Also, I'm using an IDE so it is automatically debugged I assume. Also there is no error when I run the code, so the debugger wouldn't be much help anyway. – Aria Sentori Oct 28 '19 at 04:09
  • To cricket_007: thanks, will try. – Aria Sentori Oct 28 '19 at 04:10
  • No, code is not "automatically debugged"... The debugger would be entirely useful to see **how** it runs. You said there is no output. That is a **user error**, not a compilation error. – OneCricketeer Oct 28 '19 at 04:17
  • To cricket_007: I just started learning Java this week. I have no textbooks, and I am trying to complete a college course for it. I am trying my best to learn how exactly this works, not just for a quick answer. I don't know the terminology, although I did state multiple times that the program worked as intended except for a certain input. I am sorry, because I'm a complete beginner in Java, and I don't know what I'm doing and I'm really trying so hard to understand what I'm doing wrong. Thank you for your time, and help. – Aria Sentori Oct 28 '19 at 04:25
  • That's fine. We all start somewhere, but debugging is a critical skill at all levels of coding. https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html – OneCricketeer Oct 28 '19 at 04:26

2 Answers2

1

I don't know what is wrong with your code, but I think you should be making use of the modulus here, rather than String#format:

public String printTimes(int input) {
    int seconds = input % 60;
    int minutes = (input / 60) % 60;
    int hours = (input / 3600) % 60;
    int days = input / (3600*24);

    String output = "You entered " + input + " seconds, which is " +
         days + " days, " + hours + " hours, " + minutes + " minutes and " +
         seconds + " seconds (" + days + " days " + hours + ":" + minutes + ":" +
         seconds + " hours)";

    return output;
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Originally, your program outputs nothing because you are only printing data when (Minutes < x || Seconds < x) (if either is less than 9 ... which really should be less than or equal to 9). I suspect there is more numbers than only 500000 that meet this condition.

With the new else case, it should always output some data, although, possibly incorrect.

In any case, the math can be simplified and you should remove that check entirely and just assign those padded strings immediately, even if you don't use them later

This minimal example would at least print something

....

final int minutes = (total_seconds / 60) % 60;
final int seconds = total_seconds % 60;

String paddedMinutes = String.format("%02d" , minutes);
String paddedSeconds = String.format("%02d" , seconds);

System.out.printf("You entered %d second(s), which is ", total_seconds);
if (hours == 0) {
    System.out.printf("%d minute(s), and %d second(s).\n",  minutes, seconds);
    System.out.print(paddedMinutes + ":" + paddedSeconds);
} else if ( ... ) {
    ...
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    I want to thank you so much. I think you were right about the (Minutes < x || Seconds < x). I added the padding as your showed in you example and it works just fine now. Thank you so much! – Aria Sentori Oct 28 '19 at 16:43