1

Currently when I input 1, It outputs "Standby mode is enabled" as well as "Standby Mode is disabled" and it loops the output "Standby Mode is disabled" every 5 minutes. How can I only output "Standby mode is enabled" as well as output "Standby Mode is disabled" only once in 5 minutes after I have input 1.

Here are the codes:

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class StandbyMode {

  public static void main(String[] args) {
     Scanner scanner = new Scanner(System.in);
    System.out.println("Standby Mode");
    System.out.println("Press 1 for Standby mode");
    int selectedOption = scanner.nextInt();
    if(selectedOption == 1){
      System.out.println("Standby mode is enabled");

    }else {
      System.out.println("Invalid input");

    }
    Timer t = new Timer();
    t.schedule(new TimerTask() {
        @Override
        public void run() {
           System.out.println("Standby Mode disabled");
        }
    }, 0, 300000);
    scanner.close();
  }

}

1 Answers1

0

You can use Timer within your if statement for enabled mode:

if(selectedOption == 1){
    Timer t = new Timer();
    t.schedule(new TimerTask() {
        @Override
        public void run() {
            System.out.println("Standby mode is enabled.");
        }
    }, 0, 300000);
} else {
    System.out.println("Standby mode is disabled.");
}

Note: There has to be a line drawn between disabled mode and invalid output in your definition which can be somewhat like:

else if(selectedOption==2) {
    System.out.println("Standby mode is disabled.");
} else {
    System.out.println("Invalid input");
}

Edit (after comments):: For alternating modes, you can use a flag to switch between the messages displayed by the timer as :

if (selectedOption == 1) {
    Timer t = new Timer();
    t.schedule(new TimerTask() {
        boolean flag = true;

        @Override
        public void run() {
            if (!flag) {
                System.out.println("Standby Mode Disabled");
            } else {
                System.out.println("Standby Mode Enabled");
            }
            flag = !flag;
        }
    }, 0, 3);
} else {
     System.out.println("Invalid input");
}
Naman
  • 27,789
  • 26
  • 218
  • 353
  • it loops "standby mode is enabled" and "standby mode is disabled" wasn't getting output – user8605033 Sep 13 '17 at 18:23
  • @user8605033 do you intend to alternate the output between these two? I was under an assumption they are different modes all together. – Naman Sep 13 '17 at 18:25
  • I wanted to output both "standby mode is enabled" and "standby mode is disable"(after 5 minutes). Apologies if my question was not clear – user8605033 Sep 13 '17 at 18:28
  • @user8605033 both at the same time? – Naman Sep 13 '17 at 18:29
  • Output "standby mode enabled", then after 5 minutes output "standby mode is disabled" – user8605033 Sep 13 '17 at 18:31
  • @user8605033 Updated the answer with the edit for alternate sequence which would print one Enabled message and Disabled message after 5 mins alternatively. – Naman Sep 13 '17 at 18:35
  • 1
    This helped. Thank you. is it possible to only make it output once and not loop? – user8605033 Sep 13 '17 at 18:38
  • @user8605033 Cool. Do accept it as an answer tp help other readers. Also, to output only once you can get rid of the Timer implemenation and at once you shall either output "Standby mode disabled" or output "Standby mode enabled". – Naman Sep 13 '17 at 18:42
  • seems t.cancel(); was't recognised – user8605033 Sep 13 '17 at 18:54
  • @user8605033 trying to stop the timer? https://stackoverflow.com/questions/1409116/how-to-stop-the-task-scheduled-in-java-util-timer-class – Naman Sep 13 '17 at 18:57