将用户输入附加到字符串中

时间:2013-02-05 00:51:18

标签: java string append add

我试图将用户输入的字符串附加到使用全局按键和我找到的api的字符串。但我无法弄清楚如何添加到字符串而不覆盖已存在的字符串。

int i = 1;
public String log = "Write";
public String[] logger = new String[i];
public static PrintStream out = null;
public BufferedWriter writer = null;




    public void nativeKeyPressed(NativeKeyEvent e) {
        while(i<1000000000){

        logger[i] = NativeKeyEvent.getKeyText(e.getKeyCode());
        i++;
        System.out.println(logger[i]);
        }

1 个答案:

答案 0 :(得分:2)

每当必须将字符或字符串附加到现有变量时,请使用StringBuilder类append方法。

StringBuilder strBuilder= new StringBuilder();
strBuilder.append("I am a bad programmer");
strBuilder.append("to use concatenation");
strBuilder.append("Instead of StringBuilder");
strBuilder.append(" or StringBuffer");
String str= strBuilder.toString();

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html