daniio
daniio

Reputation: 61

Can not properly use BufferedReader class

Hi I'm using eclipse and I work on windows 7 system. I would like to read some characters form keyboard using BufferedReader class. I'm using that code in Thread class:

public void run()
{
    int ch;
    while(!done)
    {
        try
        {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            ch = keyboard.read();
            System.out.println(ch);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //System.out.println(s);
        System.out.println("petla");
    }
}

The problem is that after I run the thread(It enters to the loop) and I press some buttons and confirm that using enter(\n) nothing has happen. Please help

Upvotes: 0

Views: 197

Answers (3)

daniio
daniio

Reputation: 61

Ok to precise my problem. I have a window app which looks like that: http://img846.imageshack.us/img846/5859/scaning.jpg I need press some buttons(not being focused in a JTextField) and on that basis add some position to the JTable. But while I have opened my app I can't put signs to the console. It is very simple. I just want to open my full screen program, type some characters and on that basis add position to JTable. I can't make it more clear. Thx for respond.

Upvotes: 0

daniio
daniio

Reputation: 61

Now I understand what the problem is. I was thought that when i press sign it will be buffered immediately but i have to write it to the console. I need to read pressed buttons not entered in consol. Somebody adviced me to initialize JTextField for example and read sign using that on focus. But is it elegant resolution?

Upvotes: 0

user207421
user207421

Reputation: 310985

You are losing buffered data. Don't keep creating a new BufferedReader for every character. Use the same one created before the loop.

Upvotes: 1

Related Questions