-1

I am making a clock and I obviously don't want the clock to pause while it's looking for inputs. i first use the switch-case statement which changes the value of timezone_str and then the local time method prints it So please tell me a way using which I can help the user exit my infinity loop (it's fine if it's not by inputting). || PLS NOTE: I USE BLUE J || Here is my code:-

import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Scanner;


public class AnalogClock_Project {

    public static void main(String[] args) {
        
          int choose = 0;
          boolean PutAnEndToThis = true;
          String timezone_str = new String();
           
          Scanner sc = new Scanner(System.in);
          choose = sc.nextInt();  
          
          switch(choose) {
               
              case 1:
               timezone_str = "UTC";    
              break;
              
              case 2:
              timezone_str = "GMT";
              break;
              
              case 3:
              timezone_str = "US/Central";
              break;
              
              case 4:
              timezone_str = "PST";
              break;
              
              case 5:
              timezone_str = "Asia/Kolkata";
              break;
              
              case 6:
              timezone_str = "Japan";
              break;
              
              case 7:
              timezone_str = "Europe/London";
              break;
              
              case 8:
              timezone_str = "Hongkong";
              break;
              
              case 9:
              timezone_str = "Australia/Sydney";
              break;
              
              case 10:
              timezone_str = "Europe/Rome";
              break;
              
              case 11:
              timezone_str = "US/Eastern";
              break;
              
              case 12:
              timezone_str = "US/Hawaii";
              break;

                  case 13:
                  timezone_str = "Africa/Cairo";
                  break;
                  
                  case 14:
                  timezone_str = "America/Mexico_City";
                  break;
                  
                  case 15:
                  timezone_str = "Asia/Riyadh";
                  break;
                  
                  case 16:
                  timezone_str = "Europe/Moscow";
                  break;
                  
                  case 17:
                  timezone_str = "Asia/Manila";
                  break;

           }
         
          System.out.print('\f');
          Scanner sC = new Scanner(System.in);
           
          while (PutAnEndToThis) {
 
                  LocalTime time = LocalTime.now(ZoneId.of(timezone_str));
                  System.out.print(time);
                  
                  try{
                         Thread.sleep(500);
                  } 
                  catch(InterruptedException ie) {}
 
                  System.out.print('\f'); 
                   
          }
          System.out.print("\f thank you for using you clock \n hopefully you can get rich enough \n to buy a watch next time");
        }
}
NOTred
  • 1
  • 1

2 Answers2

1

Start a separate Thread to print the time. Use Scanner to accept a value from the user. When the user enters a value, interrupt the Thread.

final String timezone_str = "UTC";
Thread thrd = new Thread(() -> {
    while (true) {
        LocalTime time = LocalTime.now(ZoneId.of(timezone_str));
        System.out.print(time);
        try {
            Thread.sleep(1000);
        }
        catch (InterruptedException ie) {
            break;
        }
        System.out.print('\f');
    }
});
thrd.start();
Scanner in = new Scanner(System.in);
in.next();
thrd.interrupt();

The user needs to press some key on the keyboard and then press <ENTER>. As soon as she does, the Thread will terminate.

Note that I used an arbitrary value for timezone_str since it was not clear to me, from your question, how you assign a value to that variable.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • I just declare it as a string then I have a switch case which determines its value for ex:- if the user enters 1 the value changes to Japan if 2 then it changes to America – NOTred Nov 09 '20 at 05:39
  • i am a noob in this can you please tell me a good way to do it now – NOTred Nov 09 '20 at 05:40
  • @NOTred if you have another question, then post a new question. – Abra Nov 09 '20 at 05:44
  • || also as a bit of extra info, this is a school project, therefore, I am bound to using blue j – NOTred Nov 09 '20 at 05:51
  • its not another question – NOTred Nov 09 '20 at 05:52
  • the program you gave is not working, and as you had asked how I have done the variable thing, i thought you could help me with it – NOTred Nov 09 '20 at 05:53
  • @NOTred `switch` does not appear in the code in your question. If you wish to add more details to your question, then you can [edit] it. You should also consider posting a [mcve] so that I can reproduce your problem. The code in my answer works for me. I can only assume that your code is different to that in my answer. – Abra Nov 09 '20 at 06:05
0

Try to change 1=1 to a boolean variable and break the loop with it.

Example:

    boolean key = true;
    while (key)
    {
        LocalTime time = LocalTime.now(ZoneId.of(timezone_str));
        System.out.print(time);
        try{
            Thread.sleep(100);
        } catch(InterruptedException ie) {
            //Do something to change key
        }

        //Do something to change key
        
    }