Java charAt() String index out of range: 0

前端 未结 5 1160
刺人心
刺人心 2021-01-13 06:51

I have this code :

private void submitPstart() {

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


    }else          


        
5条回答
  •  逝去的感伤
    2021-01-13 07:25

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

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

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

    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.

提交回复
热议问题