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!
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.