字符串索引超出范围:n [英] String index out of range: n

查看:1195
本文介绍了字符串索引超出范围:n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次执行此代码都有点问题它给我一个错误
字符串索引超出范围:'n'
n - 是否。在与此代码相关的文本框中输入的字符...
(即文本框 - t2。)它停留在第一个文本框中,检查它不会转到数组中提到的下一个文本框。 / p>

Im having a bit of a problem with this code each time i execute it it gives me an error String index out of range: 'n' n - is the no. of characters that is entered in the textbox pertaining to this code... (that is textbox - t2.)it is stuck at that first textbox checking it does not go over to the next as mentioned in the array.

Object c1[] = { t2.getText(), t3.getText(), t4.getText() };    
String b;
String f;
int counter = 0;
int d;
for(int i =0;i<=2;i++)
{
    b = c1[i].toString();
    for(int j=0;j<=b.length();j++)
    {
        d = (int)b.charAt(j);
        if((d<65 || d>90)||(d<97 || d>122))
        {
            counter++;
        }
    }
}

它基本上是一个验证码我试图做的没有例外和东西(仍然在学习的过程:) :)

it is basically a validation code that i am trying to do without exceptions and stuff(still in the process of learning :) )

任何帮助将非常感谢
thx。

any help would be appreciated thx very much.

推荐答案

使用< ,而不是< =迭代字符串时。使用< = ,当 j 等于字符串的长度时,会出现越界错误。请记住,字符串中的字符从零开始编制索引。

Use <, not <= when iterating over the string. With <=, you get an out of bounds error, when j equals the length of the string. Remember that characters in the string are indexed starting from zero.

for(int j = 0; j < b.length(); j++)

这篇关于字符串索引超出范围:n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆