Results 1 to 5 of 5
  1. #1
    adwodon is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default Printing keyboard input to screen.

    Ok so I have the following class:

    Java Code:
    package module8;
    
    import java.io.*;
    
    public class KeyBoardThread implements Runnable {
    	
    	private Thread t;
    	
    	public KeyBoardThread(){}
    	
    	@Override
    	public void run() {
    		String s = null;
    		while(t!=null){
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			try {
    				s = br.readLine();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    			if(s.contains("EXIT")){
    				stop();
    			}
    			System.out.println(s);
    			
    		}
    	}
    	
    	public void stop(){ //nullify the thread to cause any running method to stop
    		t=null;
    	}
    	
    	public void start(){ // starts new thread
    		if(t==null){
    			t = new Thread(this,"KeyBoardThread");
    			t.start();
    		}
    	}
    
    }
    I run it with this:

    Java Code:
    KeyBoardThread testKeyBoardThread = new KeyBoardThread();
    		testKeyBoardThread.start();
    		testKeyBoardThread.run();
    Its supposed to take one line of input and print it screen and the thread stops when the string "EXIT" is detected, however when I tested, it only prints to screen or acknowledges the EXIT after return has been pressed twice.

    Anyone offer any reason why its doing this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    adwodon is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    The final module of my java course is on Threads / Graphics & Applets.

    This is the first part of the exercises we were given. We have four threads running, one taking keyboard input, one reading a URL and printing the contents, one telling the time, one telling how much memory is being used.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You still need to evaluate why you are specifically calling run() on your KeyBoardThread Runnable object. Your code is effectively calling this method twice, once invoked by the Thread object when its start() method is called, and once directly called by you. Please Google and check out the Oracle tutorial on Threads for more on this, but I can't help but wonder if your directly calling this method is not messing you up.

  5. #5
    adwodon is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Yep I see what you mean now, just read the API, didnt realise the start() method would call the objects run() method.

    Thanks!
    Problem solved :)

Similar Threads

  1. Read input from keyboard
    By bison in forum New To Java
    Replies: 2
    Last Post: 11-20-2010, 06:48 PM
  2. Help with on screen keyboard Capslock
    By linkinlog0123 in forum AWT / Swing
    Replies: 2
    Last Post: 11-17-2010, 05:46 AM
  3. How to use another image using a keyboard input
    By Rekuta in forum New To Java
    Replies: 0
    Last Post: 05-13-2010, 05:00 PM
  4. get keyboard input while running in the background?
    By gen1mx6 in forum Advanced Java
    Replies: 16
    Last Post: 07-16-2009, 03:51 PM
  5. Need help with on-screen keyboard (first post)
    By tom_k in forum New To Java
    Replies: 7
    Last Post: 07-31-2008, 02:07 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •