- 2

Hi everyone i want do display hello every time i type 1 in my program,What is the required codes for that?

JAVA

14th Oct 2020, 7:54 PM
Hayabina
3 Answers
+ 1
The tip from Aleksandrs is good, but there a problem that you will get stuck because every time your terminal will ask for input. (And the rest of your program will not run). Try add then a Thread, and assign a variable input. piblic MyThread extends Thread { String input; @override public void run() { while(true) { if(input=="1") { System.out.println("hello"); input = " "; //Avoid loop } } } } public YourProgram { public static void main(String[] args) { Scanner in = new Scanner(System.in); MyThread checking = new MyThread(); checking.start(); //Do your program //When get input, do: String receiving = in.nextLine(); checking.input = receiving; //All time get new input assign checking.input. so you can do anything running your program and when match this will span. } }
15th Oct 2020, 12:17 AM
Marcelo Anjos
Marcelo Anjos - avatar
0
Create instance of Scanner class, create infinite loop, read input inside the loop, if input is 1 - print Hello.
14th Oct 2020, 8:10 PM
Aleksandrs
Aleksandrs - avatar
0
can you provide the codes?
14th Oct 2020, 11:22 PM
Hayabina