Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Maria Silva   on Nov 06 In Java Category.

  
Question Answered By: Kiet Jainukul   on Nov 06

There are several problems with your code:

First of all, events are handled by attaching a listener to an object which
fires specific events. You cannot listen to
events which are generated from the command line. Your keyReleased(),
keyPressed() and keyTyped() methods are never
called which is why the String is always null.

Second, Strings are not mutable in Java. You should use a StringBuffer instead.


Since you can't listen to key events fired from the command line (because key
events aren't fired from the command
line), if you want to create a String with all of your characters, then you
would have to it at:

c=(char)br.read();

Here is an example:


import java.io.*;

public class  IOTest {

public static  StringBuffer s = new StringBuffer();

public static void  main(String args[])
throws IOException
{
char c;
BufferedReader br =new  BufferedReader(new
InputStreamReader(System.in));
System.out.println("Entercharactes,'q' to quit.");
do
{
c=(char)br.read();
s.append(c);
}
while(c!='q');

System.out.println(s.toString());
}
}

Share: 

 
 
Didn't find what you were looking for? Find more on small problem with this code Or get search suggestion and latest updates.


Tagged: