Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java除了返回字符串外,所有方法都适用于字符串_Java_String - Fatal编程技术网

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

Java除了返回字符串外,所有方法都适用于字符串,java,string,Java,String,出于某种原因,我几乎可以对字符串执行任何方法。这包括: *获取字符串的长度 *添加到字符串中 *使用子串 *可能还有其他一切 除此之外,我无法获取字符串的值,除非使用我的drawString方法在lwjgl中绘制屏幕。在我进一步解释这个问题之前,这里是我的代码 public static boolean chatOn = false; public static String text = ""; public static float typetimer = 0; public static

出于某种原因,我几乎可以对字符串执行任何方法。这包括:

*获取字符串的长度

*添加到字符串中

*使用子串

*可能还有其他一切

除此之外,我无法获取字符串的值,除非使用我的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方法工作正常。谢谢-Tyler

如果System.out.printltext;在Text.drawStringtext之前为空,0,0;则在调用text.drawString时,text应为空。您应该检查并找出问题发生的地方,或者在代码中添加一些System.out.PrintLN,如果您特别懒惰,则跟踪代码:p。不必看文本类,我愿意打赌要打印的字符串永远不会被设置,文本正在打印一个静态字符串,或者要打印的字符串正在被文本类本身更改

从以下几点判断:

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

文本从未设置。

您是否尝试过调试以查看实际情况?是的,我已经尝试调试了一段时间。你有什么技巧来帮助调试它吗?我的意思是设置一个断点,停止执行,检查变量在打印时的内容以及drawString方法中的情况。很明显,变量的值不能自己神奇地改变,所以一定有什么东西影响它。我过去常常在按键时得到空值。我做了些什么,他们神奇地消失了。另外,如果你看,我在drawString方法之前和之后执行了print,这样就不会影响它。我创建了一个方法getText,它返回text的值,但不起作用……我宁愿推荐property而不是println。为什么?因为它只是强迫您理解代码执行过程中发生的事情。