尝试在Java中比较ASCII

尝试在Java中比较ASCII,java,Java,我不确定我在这方面哪里出了问题,错误的陈述似乎很好,但每当我试图获得正确的时候,我就会得到超出范围的错误。澄清一下,我想做的是确保ASCII码随着每个后续字母的增加而增加,如果不返回false,那么返回true public static String isOrdered(String a) { int i = 0; while (i < a.length())// Here we have a loop to compare the whole

我不确定我在这方面哪里出了问题,错误的陈述似乎很好,但每当我试图获得正确的时候,我就会得到超出范围的错误。澄清一下,我想做的是确保ASCII码随着每个后续字母的增加而增加,如果不返回false,那么返回true

public static String isOrdered(String a) {
    int i = 0;

    while (i < a.length())// Here we have a loop to compare the whole
                            // string, to make sure all the values are
                            // increasing.
    {
        char x = a.charAt(i);// Grabbing the part of the string we need to
                                // start comparing
        char y = a.charAt(i + 1);
        {
            if (x > y)// here we are comparing the value of i, to the value
                        // next in the string to make sure that the values
                        // are increasing

            {
                String answer = "false";
                return answer;
            } else if (i >= a.length()) {
                String answer = "true";
                return answer;
            }

        }
        i++;
    }

    String answer = "true";
    return answer;
}
已排序的公共静态字符串(字符串a){
int i=0;
while(iy)//这里我们比较i的值和
//下一步,确保
//正在增加
{
字符串answer=“false”;
返回答案;
}如果(i>=a.length()),则为else{
字符串answer=“true”;
返回答案;
}
}
i++;
}
字符串answer=“true”;
返回答案;
}

这里的问题是,您引用的字符位于a的字符之外。见以下部分

while (i<a.length())//Here we have a loop to compare the whole string, to make sure all the values are increasing.
{
    char x = a.charAt(i);//Grabbing the part of the string we need to start comparing
    char y = a.charAt(i+1);
    {
            if( x > y )//here we are comparing the value of i, to the value next in the string to make sure that the values are increasing
                {
                    String answer = "false";
                    return answer;
                }
                else if (i >= a.length())
                {
                    String answer = "true";
                    return answer;
                }      

       }      
       i++;
}

请看,我只循环到最后一个字符之前的字符
,而(I<(a.length()-1))
,我将对此进行更多的选择,只是因为我不能很好地离开

for( i = 1 ; i < a.length() ; i++ )
    if( a.charAt(i) > a.charAt(i-1) )
        return false ;
return true ;
for(i=1;ia.charAt(i-1))
返回false;
返回true;
在这里,我们不必担心字符串的远端溢出,只需在一个字符中开始循环,并通过查看前一个字符来执行每个测试。我还返回布尔值而不是字符串;如果你真的因为某种原因想要一个字符串,只需在其周围加上引号

每当我试图获得真值时,我都会得到超出范围的错误

问题在于:

while (i < a.length()) { //
  ...
  char y = a.charAt(i + 1); // trouble when i = a.length
...

不要链接您的代码,将其放入问题中(理想情况下,将代码最小化到尽可能小的部分)
charAt
不提供ASCII代码,而是提供UTF-16代码单位。因为在UTF-16中,一个代码点可能需要一个或两个代码单元,所以您可能希望迭代代码点而不是代码单元。你可以使用或。当然,比较代码点会给你一个字典顺序,这对人类来说很少有意义。有人真的知道或关心这一点吗₹?好的,我明白了,非常感谢,所以要澄清的是,当增加值时,它超出了导致错误输入发生的长度范围?
while (i < a.length()) { //
  ...
  char y = a.charAt(i + 1); // trouble when i = a.length
...
  public static String isOrdered(String a) {
    boolean flag = true;
    char arr[] = a.toCharArray(), t = arr[0];
    for(char c : arr)
      if(!(flag = (c == t ++)))
        break;
    return Boolean.toString(flag);
  }