0

Actually I'm using threads to print two names simultaneously and I want to stop that execution after pressing the 'e' letter. But I don't want to press 'e' and then hit Enter. I just want to stop by pressing letter 'e'.

import java.io.*;
class multithreads{
    public static void main(String[] args) {


        thread4 t1=new thread4("firstname",500,500);

        thread4 t2=new thread4("Secondname",1000,0);

        thread4 t3=new thread4("exit",10,0);


        }
}

class thread4 implements Runnable{

static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Thread t;
String name;
int msg='c';
static boolean over=true;
int t1;
int t2;
thread4(String s,int a,int b){
    name=s;
    t1=a;
    t2=b;
    t=new Thread(this,s);
    t.start();

}

public void run(){




    while(over){

    if(name.equals("exit")){


        try{
            msg=in.read();
            try{
            t.sleep(t1);

        }
        catch(InterruptedException e){
            System.out.println(e);
        }



        }catch(Exception eio){
            System.out.println(eio);
        }

        if(msg=='E'){
            over=false; 
        }else{
            msg='c';
        }


    }else{



        try{
            t.sleep(t1);

        }
        catch(InterruptedException e){
            System.out.println(e);
        }

        System.out.print(name);
        for(int j=name.length();j>0;j--){


        System.out.print("\b\b\b"); 
        }
        try{
            t.sleep(t2);

        }
        catch(InterruptedException e){
            System.out.println(e);
        }
        }


        }

    }
}
  • 1
    Welcome to Stack Overflow! To help you get the most out of this site, please take a moment to read our [SO Question Checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) and rework your question accordingly. – Joe C Sep 28 '16 at 05:07
  • Possible duplicate : http://stackoverflow.com/questions/17555515/how-to-get-input-without-pressing-enter-every-time – Jitin Kodian Sep 28 '16 at 05:20

1 Answers1

1

There is no such possibility. We cannot add something like a key listener to the console - it is not our program. The only thing to input the data is to hit Enter.

Wojtek
  • 1,288
  • 11
  • 16