Java charAt() 字符串索引超出范围:0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13118732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:33:58  来源:igfitidea点击:

Java charAt() String index out of range: 0

java

提问by user1782426

I have this code :

我有这个代码:

private void submitPstart() {

    if (tStock.getText().charAt(0)>='A' && tStock.getText().charAt(0)<='Z'){


    }else {
        errorBox ("Uppercase A-Z");
    }

    tStock.setText("");
    tStock.setFocus();
}

THis is working but when I try not to put anything on the textbox and press the OK button it crashes. It says:

这是有效的,但是当我尝试不在文本框上放置任何内容并按下“确定”按钮时,它会崩溃。它说:

java.lang.StringIndexOutOfBoundsException: String index out of range: 0

java.lang.StringIndexOutOfBoundsException: String index out of range: 0

and it pointed out to this part: if (tStock.getText().charAt(0)>='A' && tStock.getText().charAt(0)<='Z')

它指出这一部分: if (tStock.getText().charAt(0)>='A' && tStock.getText().charAt(0)<='Z')

Any help is appreciated. Thanks

任何帮助表示赞赏。谢谢

采纳答案by Subhrajyoti Majumder

you must check null and length greater than 0.

您必须检查空值和长度是否大于 0。

 if (tStockPIStart!=null && tStockPIStart.getText().length()>0 && tStockPIStart.getText().charAt(0)>='A' && tStockPIStart.getText().charAt(0)<='Z'){

回答by Brian Agnew

You need to check if getText()returns a 0-length (i.e. empty) string.

您需要检查是否getText()返回 0 长度(即空)字符串。

If it does, then don't try to pull the first character out! (via charAt())

如果是这样,那么不要试图拉出第一个字符!(通过charAt())

Note that your commented-out check for length()should occur priorto the existing character check.

请注意,注释掉的检查length()应在现有字符检查之前进行。

You may want to check for a null string being returned as well, depending on your framework/solution etc. Note the Apache Commons StringUtils.isEmpty()method, which performs this check concisely.

您可能还想检查返回的空字符串,具体取决于您的框架/解决方案等。请注意Apache Commons StringUtils.isEmpty()方法,该方法简洁地执行此检查。

回答by Sujay

Try

尝试

if (tStockPIStart.getText().length() > 0 && tStockPIStart.getText().charAt(0)>='A' && tStockPIStart.getText().charAt(0)<='Z')

In your case, if the text is empty, then the length returned will be 0. Hence the charAt(..) method will throw you an exception. As such, you should first check that the text that you're trying to compare is empty or not.

在您的情况下,如果文本为空,则返回的长度将为 0。因此 charAt(..) 方法将向您抛出异常。因此,您应该首先检查您尝试比较的文本是否为空。

回答by koljaTM

Add

添加

if (tStockPIStart!=null && tStockPIStart.length>0) {
    [...]
}

回答by RNJ

In Java 7 there is the isEmptymethod you can use to make things a bit more expressive in your code

在 Java 7 中,您可以使用isEmpty方法使代码中的内容更具表现力

if (tStockPIStart.getText()!=null && !tStockPIStart.getText().isEmpty()) {
    //do stuff
}

This is the same as doing length != 0but I personally think is a bit more clear.

这和做一样,length != 0但我个人认为更清楚一点。