除了返回字符串之外,Java所有方法都适用于字符串

时间:2014-11-14 23:29:43

标签: java string

由于某种原因,我可以为字符串做任何方法。这包括:

*获取字符串的长度

*添加到字符串

*使用子字符串

*可能是其他所有人

除了使用我的drawString方法在lwjgl中绘制到屏幕之外,我无法获取字符串的值。在我进一步解释这个问题之前,这是我的代码。

public static boolean chatOn = false;
public static String text = "";
public static float typetimer = 0;
public static int ctrltimer = 0;
public static boolean runcmd = false;

public static void chat() {
    if (Keyboard.isKeyDown(Keyboard.KEY_TAB)) {
        if (ctrltimer < 0) {
            chatOn = !chatOn;
            Keyboard.destroy();
            try {Keyboard.create();} catch (LWJGLException e) {}
            ctrltimer = 10;
        }
    }
    ctrltimer -= Game.delta;
    typetimer -= Game.delta;
    if (chatOn) {
        //try {text.replace(null, "happy");} catch(NullPointerException e) {}
        System.out.println(text);//print to console, dosen't
        Text.drawString(text, 0, 0);//write the text on the screen with my draw method, does work
        System.out.println(text);//print to console, dosen't, yet the one drawstring worked
        if (typetimer < 0) {
            while (Keyboard.next()) {
            try {
                    if (Keyboard.getEventKey() == Keyboard.KEY_BACK) {
                        text = text.substring(0, text.length()-1);
                        typetimer = 1;
                        System.out.println(text);//print to console, doesn't
                    }
                    else if (Keyboard.getEventKey() == Keyboard.KEY_RETURN) {
                        System.out.println(text);//print to console, doesn't
                        runCommand();
                        text = "";
                        chatOn = false;
                    } 
                    else {
                        System.out.println(text);//print to console, doesn't
                        text = text + Keyboard.getEventCharacter();
                    }
                    typetimer = 10;
            } catch(Exception e){

            }
            }
        }
    }
}

public static void runCommand() {
    String command = text;
    System.out.println(command);//print to console, doesnt
    if (command.startsWith("time")) {
        try {
        String[] time = new String[1];
        time = command.split(" ", 0);
        Camera.nighttimeASecond = Integer.parseInt(time[0]);
        } catch (Exception e){
            System.out.println("could not set time");
        }
    }
}

如果您在代码中阅读了我的笔记,您可以看到我放置print方法和drawString方法的位置。虽然print方法工作正常,但drawString方法没有打印任何内容,有时可能会打印字符串的前几个单词。谢谢 - 泰勒

1 个答案:

答案 0 :(得分:0)

如果在调用System.out.println(text);之前Text.drawString(text, 0, 0);为空,则在调用textText.drawString()应为空。您应该follow mattias' suggested debugging guide并找出问题发生的位置(或者在System.out.println()之后添加一些text代码,如果您特别懒,则跟踪它:p)。如果没看一下你的Text类,我愿意打赌要打印的字符串永远不会被设置,Text正在打印一个静态字符串,或者要打印的字符串正在被Text类本身改变。

判断:

String command = text;
System.out.println(command);//print to console, doesnt

text永远不会被设定。