: : : :
This message was edited by Vilanye at 2005-9-25 0:58:5
: : : : 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.
: : : :
: : : :
: : : :
Just my 2 bits
: : : :
: : : :
: : : :
: : : :
: : : 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.
: : Just my 2 bits
: :
: :
: 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:
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
Im sure that if you look at the code with an open mind, you'll understand my method. Good luck!