- 2
Hi everyone i want do display hello every time i type 1 in my program,What is the required codes for that?
JAVA
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.
}
}
0
Create instance of Scanner class, create infinite loop, read input inside the loop, if input is 1 - print Hello.
0
can you provide the codes?