Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Converting Seconds into Minutes, Hours and Seconds... HELP!(PartII)

KdohKdoh Posts: 13Member
Well this is for the second part of the assignment.. Fock.
I dont know why I can't seem to figure this out.
But I think once I get these two down.. I should have a pretty good understanding.
Now my program is prompting me to INITIALIZE my variables.. and I dont understand why.

Here's what I got. What I'm suposed to be able to do with this one, is type in a value..
Total Seconds:___
And then Convert it into
Hours:
MinuteS:
Seconds:

If you could help me out with why this isnt working, that would be awesome.
import
java.util.Scanner;

class StoHMS
{
public static void main (String[] args) // Main input.
{
int minutes, hours, seconds, timeInSeconds = 0; //declaration of variables.
timeInSeconds = timeInSeconds - (hours * 3600);
Scanner scan = new Scanner (System.in); // declaration of scanner class.

System.out.print ("Enter the number of seconds: ");
timeInSeconds = scan.nextInt();



hours = timeInSeconds / 3600;
timeInSeconds += minutes * 60;
timeInSeconds += seconds;

System.out.println(hours + " hours: ");
System.out.println( + minutes + " minutes: ");
System.out.println( + seconds + " seconds");

}
}

Comments

  • VilanyeVilanye Posts: 684Member
    [b][red]This message was edited by Vilanye at 2005-9-25 0:58:5[/red][/b][hr]
    Lets walk through your program:

    int minutes, hours, seconds, timeInSeconds = 0;

    The reason you are getting the errors that minutes, hours and seconds might not have been initialized is because only timeInSeconds was initialized to 0. You have to explicitly initialize each one

    int minutes=0, hours=0, seconds=0, timeInSeconds = 0;//this will fix the compiler errors.

    From here on, I will assume that the corrected line above is part of your program. This will help point out your logic errors. If you had used minutes and seconds the way it should have been, the compiler would not have cared if they were initialized. It only cares when you try to use them uninitialized.

    What is the point of this next line?

    timeInSeconds = timeInSeconds - (hours * 3600);

    At this point timeInSeconds and hours are both 0. 0-(0*3600) is 0. This does nothing. What are you trying to accomplish here?

    Next 3 lines:

    Scanner scan = new Scanner (System.in); // declaration of scanner class.

    System.out.print ("Enter the number of seconds: ");
    timeInSeconds = scan.next();

    This is fine, but timeInSeconds overwrites what was done earlier. If the line right after the declaration and initialization actually did anything, that work would be wiped out here.


    hours = timeInSeconds / 3600;//no problem here use this line to fix the next line

    timeInSeconds += minutes * 60;//not quite

    The above line is equivalent to timeInSeconds = timeInSeconds + minutes * 60. Assuming that the user entered 30, it would be timeInSeconds = 30 + 0 *60. Which is 30, exactly what was entered by the user and not what you need. In short this does nothing.

    Presumably you want to convert seconds into minutes. Rethink the arithmetic using the way you got hours as a guide.

    timeInSeconds += seconds;//this is pointless

    The way you have the program written this is timeInSeconds = timeInSeconds + seconds. seconds is set to 0. So it changes the value to the exact value it was. You really do not need a seconds variable since timeInSeconds will be equivalent.

    System.out.println(hours + " hours: ");//this is fine
    System.out.println( + minutes + " minutes: ");//This might cause a compiler error
    System.out.println( + seconds + " seconds");//ditto, instead of using seconds, just use timeInSeconds.

    I am not sure if the compiler will complain about the last 2 lines. + is the concatenation operator, it is a binary operator, meaning it uses 2 operands. + minutes is only one operand, lose the first + on the last 2 lines. Even if it does compile, ditch the first + on those lines.


    Like I said before tracing the code on paper is extremely helpful. If you would have spent 10 minutes doing this you likely would have figured out the semantic and syntax errors on your own.


    [italic][blue]Just my 2 bits[/blue][italic]



  • KdohKdoh Posts: 13Member
    : [b][red]This message was edited by Vilanye at 2005-9-25 0:58:5[/red][/b][hr]
    : Lets walk through your program:
    :
    : int minutes, hours, seconds, timeInSeconds = 0;
    :
    : The reason you are getting the errors that minutes, hours and seconds might not have been initialized is because only timeInSeconds was initialized to 0. You have to explicitly initialize each one
    :
    : int minutes=0, hours=0, seconds=0, timeInSeconds = 0;//this will fix the compiler errors.
    :
    : From here on, I will assume that the corrected line above is part of your program. This will help point out your logic errors. If you had used minutes and seconds the way it should have been, the compiler would not have cared if they were initialized. It only cares when you try to use them uninitialized.
    :
    : What is the point of this next line?
    :
    : timeInSeconds = timeInSeconds - (hours * 3600);
    :
    : At this point timeInSeconds and hours are both 0. 0-(0*3600) is 0. This does nothing. What are you trying to accomplish here?
    :
    : Next 3 lines:
    :
    : Scanner scan = new Scanner (System.in); // declaration of scanner class.
    :
    : System.out.print ("Enter the number of seconds: ");
    : timeInSeconds = scan.next();
    :
    : This is fine, but timeInSeconds overwrites what was done earlier. If the line right after the declaration and initialization actually did anything, that work would be wiped out here.
    :
    :
    : hours = timeInSeconds / 3600;//no problem here use this line to fix the next line
    :
    : timeInSeconds += minutes * 60;//not quite
    :
    : The above line is equivalent to timeInSeconds = timeInSeconds + minutes * 60. Assuming that the user entered 30, it would be timeInSeconds = 30 + 0 *60. Which is 30, exactly what was entered by the user and not what you need. In short this does nothing.
    :
    : Presumably you want to convert seconds into minutes. Rethink the arithmetic using the way you got hours as a guide.
    :
    : timeInSeconds += seconds;//this is pointless
    :
    : The way you have the program written this is timeInSeconds = timeInSeconds + seconds. seconds is set to 0. So it changes the value to the exact value it was. You really do not need a seconds variable since timeInSeconds will be equivalent.
    :
    : System.out.println(hours + " hours: ");//this is fine
    : System.out.println( + minutes + " minutes: ");//This might cause a compiler error
    : System.out.println( + seconds + " seconds");//ditto, instead of using seconds, just use timeInSeconds.
    :
    : I am not sure if the compiler will complain about the last 2 lines. + is the concatenation operator, it is a binary operator, meaning it uses 2 operands. + minutes is only one operand, lose the first + on the last 2 lines. Even if it does compile, ditch the first + on those lines.
    :
    :
    : Like I said before tracing the code on paper is extremely helpful. If you would have spent 10 minutes doing this you likely would have figured out the semantic and syntax errors on your own.
    :
    :
    : [italic][blue]Just my 2 bits[/blue][italic]
    :
    :
    :
    :
    Ok, Thanks man. I appreciate your help alot. As just the slightest little bit of help, can goo a long way in JAVA.

    Only problem is I think I do need a seconds variable don't I?
    I mean if I'm asked to have a seconds amount.
    Hours:
    Minutes:
    Seconds:

    Right now it will convert the hours, convert the minutes... but won't seem to convert the seconds.

    And your saying I dont need a seconds variable?
    Why is this man?
  • VilanyeVilanye Posts: 684Member
    : : [b][red]This message was edited by Vilanye at 2005-9-25 0:58:5[/red][/b][hr]
    : : Lets walk through your program:
    : :
    : : int minutes, hours, seconds, timeInSeconds = 0;
    : :
    : : The reason you are getting the errors that minutes, hours and seconds might not have been initialized is because only timeInSeconds was initialized to 0. You have to explicitly initialize each one
    : :
    : : int minutes=0, hours=0, seconds=0, timeInSeconds = 0;//this will fix the compiler errors.
    : :
    : : From here on, I will assume that the corrected line above is part of your program. This will help point out your logic errors. If you had used minutes and seconds the way it should have been, the compiler would not have cared if they were initialized. It only cares when you try to use them uninitialized.
    : :
    : : What is the point of this next line?
    : :
    : : timeInSeconds = timeInSeconds - (hours * 3600);
    : :
    : : At this point timeInSeconds and hours are both 0. 0-(0*3600) is 0. This does nothing. What are you trying to accomplish here?
    : :
    : : Next 3 lines:
    : :
    : : Scanner scan = new Scanner (System.in); // declaration of scanner class.
    : :
    : : System.out.print ("Enter the number of seconds: ");
    : : timeInSeconds = scan.next();
    : :
    : : This is fine, but timeInSeconds overwrites what was done earlier. If the line right after the declaration and initialization actually did anything, that work would be wiped out here.
    : :
    : :
    : : hours = timeInSeconds / 3600;//no problem here use this line to fix the next line
    : :
    : : timeInSeconds += minutes * 60;//not quite
    : :
    : : The above line is equivalent to timeInSeconds = timeInSeconds + minutes * 60. Assuming that the user entered 30, it would be timeInSeconds = 30 + 0 *60. Which is 30, exactly what was entered by the user and not what you need. In short this does nothing.
    : :
    : : Presumably you want to convert seconds into minutes. Rethink the arithmetic using the way you got hours as a guide.
    : :
    : : timeInSeconds += seconds;//this is pointless
    : :
    : : The way you have the program written this is timeInSeconds = timeInSeconds + seconds. seconds is set to 0. So it changes the value to the exact value it was. You really do not need a seconds variable since timeInSeconds will be equivalent.
    : :
    : : System.out.println(hours + " hours: ");//this is fine
    : : System.out.println( + minutes + " minutes: ");//This might cause a compiler error
    : : System.out.println( + seconds + " seconds");//ditto, instead of using seconds, just use timeInSeconds.
    : :
    : : I am not sure if the compiler will complain about the last 2 lines. + is the concatenation operator, it is a binary operator, meaning it uses 2 operands. + minutes is only one operand, lose the first + on the last 2 lines. Even if it does compile, ditch the first + on those lines.
    : :
    : :
    : : Like I said before tracing the code on paper is extremely helpful. If you would have spent 10 minutes doing this you likely would have figured out the semantic and syntax errors on your own.
    : :
    : :
    : : [italic][blue]Just my 2 bits[/blue][italic]
    : :
    : :
    : :
    : :
    : Ok, Thanks man. I appreciate your help alot. As just the slightest little bit of help, can goo a long way in JAVA.
    :
    : Only problem is I think I do need a seconds variable don't I?
    : I mean if I'm asked to have a seconds amount.
    : Hours:
    : Minutes:
    : Seconds:
    :
    : Right now it will convert the hours, convert the minutes... but won't seem to convert the seconds.
    :
    : And your saying I dont need a seconds variable?
    : Why is this man?
    :


    If you are asked to use a seconds variable, then use one. You don't need it, because timeInSeconds is exactly the same as seconds.

    System.out.print ("Enter the number of seconds: ");
    timeInSeconds = scan.next();

    You ask for the number of seconds. What does converting timeInSeconds to seconds accomplish? But if it is part of the program requirements, then you have little choice.
    [italic][blue]Just my 2 bits[/blue][italic]

  • KdohKdoh Posts: 13Member
    : : : [b][red]This message was edited by Vilanye at 2005-9-25 0:58:5[/red][/b][hr]
    : : : Lets walk through your program:
    : : :
    : : : int minutes, hours, seconds, timeInSeconds = 0;
    : : :
    : : : The reason you are getting the errors that minutes, hours and seconds might not have been initialized is because only timeInSeconds was initialized to 0. You have to explicitly initialize each one
    : : :
    : : : int minutes=0, hours=0, seconds=0, timeInSeconds = 0;//this will fix the compiler errors.
    : : :
    : : : From here on, I will assume that the corrected line above is part of your program. This will help point out your logic errors. If you had used minutes and seconds the way it should have been, the compiler would not have cared if they were initialized. It only cares when you try to use them uninitialized.
    : : :
    : : : What is the point of this next line?
    : : :
    : : : timeInSeconds = timeInSeconds - (hours * 3600);
    : : :
    : : : At this point timeInSeconds and hours are both 0. 0-(0*3600) is 0. This does nothing. What are you trying to accomplish here?
    : : :
    : : : Next 3 lines:
    : : :
    : : : Scanner scan = new Scanner (System.in); // declaration of scanner class.
    : : :
    : : : System.out.print ("Enter the number of seconds: ");
    : : : timeInSeconds = scan.next();
    : : :
    : : : This is fine, but timeInSeconds overwrites what was done earlier. If the line right after the declaration and initialization actually did anything, that work would be wiped out here.
    : : :
    : : :
    : : : hours = timeInSeconds / 3600;//no problem here use this line to fix the next line
    : : :
    : : : timeInSeconds += minutes * 60;//not quite
    : : :
    : : : The above line is equivalent to timeInSeconds = timeInSeconds + minutes * 60. Assuming that the user entered 30, it would be timeInSeconds = 30 + 0 *60. Which is 30, exactly what was entered by the user and not what you need. In short this does nothing.
    : : :
    : : : Presumably you want to convert seconds into minutes. Rethink the arithmetic using the way you got hours as a guide.
    : : :
    : : : timeInSeconds += seconds;//this is pointless
    : : :
    : : : The way you have the program written this is timeInSeconds = timeInSeconds + seconds. seconds is set to 0. So it changes the value to the exact value it was. You really do not need a seconds variable since timeInSeconds will be equivalent.
    : : :
    : : : System.out.println(hours + " hours: ");//this is fine
    : : : System.out.println( + minutes + " minutes: ");//This might cause a compiler error
    : : : System.out.println( + seconds + " seconds");//ditto, instead of using seconds, just use timeInSeconds.
    : : :
    : : : I am not sure if the compiler will complain about the last 2 lines. + is the concatenation operator, it is a binary operator, meaning it uses 2 operands. + minutes is only one operand, lose the first + on the last 2 lines. Even if it does compile, ditch the first + on those lines.
    : : :
    : : :
    : : : Like I said before tracing the code on paper is extremely helpful. If you would have spent 10 minutes doing this you likely would have figured out the semantic and syntax errors on your own.
    : : :
    : : :
    : : : [italic][blue]Just my 2 bits[/blue][italic]
    : : :
    : : :
    : : :
    : : :
    : : Ok, Thanks man. I appreciate your help alot. As just the slightest little bit of help, can goo a long way in JAVA.
    : :
    : : Only problem is I think I do need a seconds variable don't I?
    : : I mean if I'm asked to have a seconds amount.
    : : Hours:
    : : Minutes:
    : : Seconds:
    : :
    : : Right now it will convert the hours, convert the minutes... but won't seem to convert the seconds.
    : :
    : : And your saying I dont need a seconds variable?
    : : Why is this man?
    : :
    :
    :
    : If you are asked to use a seconds variable, then use one. You don't need it, because timeInSeconds is exactly the same as seconds.
    :
    : System.out.print ("Enter the number of seconds: ");
    : timeInSeconds = scan.next();
    :
    : You ask for the number of seconds. What does converting timeInSeconds to seconds accomplish? But if it is part of the program requirements, then you have little choice.
    : [italic][blue]Just my 2 bits[/blue][italic]
    :
    :
    Well I need to get an answer with the remaining seconds.
    Like for example:
    Enter the number of seconds: 9999

    Hours: 2
    Minutes: 46
    Seconds: 39

    I do need the seconds.. I think!?

    Right now my only problem is getting those remaining seconds.

    Right now its just converting the hours.. and then the minutes, like it is doing the individual calculations. Like 9999/ 3600.. for hours.. and then doing 9999/ 60. And the seconds just keep equaling the number I typed in. This is not what I want at all. I want it to cary over the previous number.

    I'm totally confused now...
  • VilanyeVilanye Posts: 684Member

    : Well I need to get an answer with the remaining seconds.
    : Like for example:
    : Enter the number of seconds: 9999
    :
    : Hours: 2
    : Minutes: 46
    : Seconds: 39
    :
    : I do need the seconds.. I think!?
    :
    : Right now my only problem is getting those remaining seconds.
    :
    : Right now its just converting the hours.. and then the minutes, like it is doing the individual calculations. Like 9999/ 3600.. for hours.. and then doing 9999/ 60. And the seconds just keep equaling the number I typed in. This is not what I want at all. I want it to cary over the previous number.
    :
    : I'm totally confused now...
    :

    ok, I understand now. Sorry about confusing you more. Yes you need seconds. Your problem is a simple arithmetic one.

    So to clarify, what you want is if the user entered 7500 seconds, you want to output 2 hours 5 minutes and 0 seconds, right?

    What you need to do is after calculating how many hours, is subtract the number of seconds in the amount of hours, and store it in time in seconds. Then use the new value to figure out minutes, subtract the number of seconds in the amount of minutes, and store that in timeInSeconds. At this point, the value in timeInSeconds is equal to seconds.

    What you were doing, and where I got confused was getting hours and minutes from the same value, not what you need presumably. So with 7500 seconds you would get 2 hours and 125 minutes.

    Here is a little bit of the code

    hours = timeInSeconds/3600;
    timeInSeconds = timeInSeconds -(hours*3600);
    //get minutes next using the same steps, but different values of course
    ...

    If 7500 was entered, hours would hold 2.
    The next line would assign 300 to timeInSeconds.

    If this looked remarkably similar to what you had earlier, you are correct. I misunderstood what you were doing, and made life harder on you. I apologize.
  • anthrax11anthrax11 Posts: 511Member
    : : : : [b][red]This message was edited by Vilanye at 2005-9-25 0:58:5[/red][/b][hr]
    : : : : Lets walk through your program:
    : : : :
    : : : : int minutes, hours, seconds, timeInSeconds = 0;
    : : : :
    : : : : The reason you are getting the errors that minutes, hours and seconds might not have been initialized is because only timeInSeconds was initialized to 0. You have to explicitly initialize each one
    : : : :
    : : : : int minutes=0, hours=0, seconds=0, timeInSeconds = 0;//this will fix the compiler errors.
    : : : :
    : : : : From here on, I will assume that the corrected line above is part of your program. This will help point out your logic errors. If you had used minutes and seconds the way it should have been, the compiler would not have cared if they were initialized. It only cares when you try to use them uninitialized.
    : : : :
    : : : : What is the point of this next line?
    : : : :
    : : : : timeInSeconds = timeInSeconds - (hours * 3600);
    : : : :
    : : : : At this point timeInSeconds and hours are both 0. 0-(0*3600) is 0. This does nothing. What are you trying to accomplish here?
    : : : :
    : : : : Next 3 lines:
    : : : :
    : : : : Scanner scan = new Scanner (System.in); // declaration of scanner class.
    : : : :
    : : : : System.out.print ("Enter the number of seconds: ");
    : : : : timeInSeconds = scan.next();
    : : : :
    : : : : This is fine, but timeInSeconds overwrites what was done earlier. If the line right after the declaration and initialization actually did anything, that work would be wiped out here.
    : : : :
    : : : :
    : : : : hours = timeInSeconds / 3600;//no problem here use this line to fix the next line
    : : : :
    : : : : timeInSeconds += minutes * 60;//not quite
    : : : :
    : : : : The above line is equivalent to timeInSeconds = timeInSeconds + minutes * 60. Assuming that the user entered 30, it would be timeInSeconds = 30 + 0 *60. Which is 30, exactly what was entered by the user and not what you need. In short this does nothing.
    : : : :
    : : : : Presumably you want to convert seconds into minutes. Rethink the arithmetic using the way you got hours as a guide.
    : : : :
    : : : : timeInSeconds += seconds;//this is pointless
    : : : :
    : : : : The way you have the program written this is timeInSeconds = timeInSeconds + seconds. seconds is set to 0. So it changes the value to the exact value it was. You really do not need a seconds variable since timeInSeconds will be equivalent.
    : : : :
    : : : : System.out.println(hours + " hours: ");//this is fine
    : : : : System.out.println( + minutes + " minutes: ");//This might cause a compiler error
    : : : : System.out.println( + seconds + " seconds");//ditto, instead of using seconds, just use timeInSeconds.
    : : : :
    : : : : I am not sure if the compiler will complain about the last 2 lines. + is the concatenation operator, it is a binary operator, meaning it uses 2 operands. + minutes is only one operand, lose the first + on the last 2 lines. Even if it does compile, ditch the first + on those lines.
    : : : :
    : : : :
    : : : : Like I said before tracing the code on paper is extremely helpful. If you would have spent 10 minutes doing this you likely would have figured out the semantic and syntax errors on your own.
    : : : :
    : : : :
    : : : : [italic][blue]Just my 2 bits[/blue][italic]
    : : : :
    : : : :
    : : : :
    : : : :
    : : : Ok, Thanks man. I appreciate your help alot. As just the slightest little bit of help, can goo a long way in JAVA.
    : : :
    : : : Only problem is I think I do need a seconds variable don't I?
    : : : I mean if I'm asked to have a seconds amount.
    : : : Hours:
    : : : Minutes:
    : : : Seconds:
    : : :
    : : : Right now it will convert the hours, convert the minutes... but won't seem to convert the seconds.
    : : :
    : : : And your saying I dont need a seconds variable?
    : : : Why is this man?
    : : :
    : :
    : :
    : : If you are asked to use a seconds variable, then use one. You don't need it, because timeInSeconds is exactly the same as seconds.
    : :
    : : System.out.print ("Enter the number of seconds: ");
    : : timeInSeconds = scan.next();
    : :
    : : You ask for the number of seconds. What does converting timeInSeconds to seconds accomplish? But if it is part of the program requirements, then you have little choice.
    : : [italic][blue]Just my 2 bits[/blue][italic]
    : :
    : :
    : Well I need to get an answer with the remaining seconds.
    : Like for example:
    : Enter the number of seconds: 9999
    :
    : Hours: 2
    : Minutes: 46
    : Seconds: 39
    :
    : I do need the seconds.. I think!?
    :
    : Right now my only problem is getting those remaining seconds.
    :
    : Right now its just converting the hours.. and then the minutes, like it is doing the individual calculations. Like 9999/ 3600.. for hours.. and then doing 9999/ 60. And the seconds just keep equaling the number I typed in. This is not what I want at all. I want it to cary over the previous number.
    :
    : I'm totally confused now...
    :

    I'm not in the mood of real coding right now ,so I'll write some pseudo code:
    [code]
    inputsecs = 9999
    secs = 0
    mins = 0
    hours = 0

    //Now we need to add 3600 until it exceeds inputsecs(9999)
    add_3600_again:
    if secs + 3600 > inputsecs then goto hours_done
    hours = hours + 1
    secs = secs + 3600
    goto add_3600_again
    hours_done:
    //hours = 2

    inputsecs = inputsecs - hours * 3600 //9999 - 7200 = 2799

    //Now we need to add 60 until it exceeds inputsecs(2799)

    secs = 0
    add_60_again:
    if secs + 60 > inputsecs then goto mins_done
    secs = secs + 60
    mins = mins + 1
    goto add_60_again
    mins_done:
    //mins = 46

    inputsecs = inputsecs - mins * 60 //2799 - 2760 = 39

    //and its done!
    hours = 2
    mins = 46
    inputsecs = 39
    [/code]
    Im sure that if you look at the code with an open mind, you'll understand my method. Good luck!
Sign In or Register to comment.