Java Code Not Doing What I Want It To Do!
The code is supposed to be a timer that stops at the specific minute, second, and hour specified by the user. However, it does not stop and continues without saying anything. This is the code please help!
Code Java:
import java.util.Scanner;
public class Timer {
public int seconds = 0;
public int minutes = 0;
public int hours = 0;
private Scanner hourstop;
private Scanner minstop;
private Scanner secstop;
public int counting() throws InterruptedException{
for(seconds = 0; ;){
seconds++;
Thread.sleep(1000);
if(seconds >= 60){
minutes++;
seconds = 0;
if(minutes >= 60){
hours++;
minutes = 0;
}
}
System.out.printf("%02d", hours);
System.out.print(":");
System.out.printf("%02d", minutes);
System.out.print(":");
System.out.printf("%02d", seconds);
System.out.println(" ");
}
}
public boolean timesUp() throws InterruptedException{
hourstop = new Scanner(System.in);
minstop = new Scanner(System.in);
secstop = new Scanner(System.in);
System.out.println("Set timer field hour to: ");
int x = hourstop.nextInt();
System.out.println("Set timer field minutes to: ");
int y = minstop.nextInt();
System.out.println("Set timer field seconds to: ");
int z = secstop.nextInt();
boolean bool = false;
if(x == hours && y == minutes && z == seconds)
{
bool = true;
}
if (bool == true)
{
String TimeisUp = new String("Timer is finished counting");
System.out.println(TimeisUp);
}
System.out.println(counting());
return bool;
}
public static void main(String args[]) throws InterruptedException{
Timer t = new Timer();
System.out.println(t.timesUp());
}
}
Output:
//System output
Set timer field hour to:
//User Input
0
//System output
Set timer field minutes to:
//User Input
0
//System output
Set timer field seconds to:
//User Input
1
//System output
00:00:01
00:00:02
00:00:03
00:00:04
00:00:05
Output (different x,y, and z):
//System output
Set timer field hour to:
//User Input
0
//System output
Set timer field minutes to:
//User Input
0
//System output
Set timer field seconds to:
//User Input
0
//System output
Timer is finished counting
00:00:01
00:00:02
00:00:03
Expected Output:
//System output
Set timer field hour to:
//User Input
0
//System output
Set timer field minutes to:
//User Input
0
//System output
Set timer field seconds to:
//User Input
10
//System output
00:00:01 //erases this line then prints next line
00:00:02 //erases this line then prints next line
00:00:03 //erases this line then prints next line
00:00:04 //erases this line then prints next line
00:00:05 //erases this line then prints next line
00:00:06 //erases this line then prints next line
00:00:07 //erases this line then prints next line
00:00:08 //erases this line then prints next line
00:00:09 //erases this line then prints next line
Timer is finished counting
At that the point program would exit.
Re: Java Code Not Doing What I Want It To Do!
Please see the announcements page for the use of code tags when posting code on the forum.
Quote:
it does not stop and continues without saying anything
What did you expect it to say? How do you know it does not stop? Add some printlns to verify what is running and what is not running.
Re: Java Code Not Doing What I Want It To Do!
It works for 00:00:00 and detects if boolean is true or not and it does print for that time but anything else it wont print the String. I know it does not stop because after it says the string for 00:00:00 it keeps counting up. I want it to stop when it reaches the specified x,y, and z and print the String.
Re: Java Code Not Doing What I Want It To Do!
Please see the link in the previous post for the use of code tags, and edit your post to include code tags. Be sure the formatting is also correct. More people will look at the code and you will potentially get faster/better answers if people can read the code.
If the code takes input, provide the exact input used and the exact output received from that input, and then post the output you expected instead
Re: Java Code Not Doing What I Want It To Do!
I have done what you have said, can you please respond with a solution to this problem?
Re: Java Code Not Doing What I Want It To Do!
Where do you plan to break out of the infinite loop in the counting method? ..I see no way to exit that loop.
Re: Java Code Not Doing What I Want It To Do!
When x,y, and z were equal to the hour, minute, and second fields of the timer.
Re: Java Code Not Doing What I Want It To Do!
Where is the code that will exit the loop? Can you post the statements to do that?
Re: Java Code Not Doing What I Want It To Do!
That is what I need help doing. I want it to exit the loop when it reaches the inputted x,y, and z and say the String!
Re: Java Code Not Doing What I Want It To Do!
Here's a tip I always use while debugging:
*) Identify the type of error (syntax, logical or run-time).
Once you have identified the type or error it'll be much easier to find what's not working correctly.
Re: Java Code Not Doing What I Want It To Do!
Quote:
I want it to exit the loop when
Use an if statement to determine when the conditions have been met and then use a break statement to exit the loop.