试图比较Java中的ASCII

时间:2015-08-29 00:54:33

标签: 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
                            // 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;
}

3 个答案:

答案 0 :(得分:2)

这里的问题是你引用一个超出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++;
}

这里你的单词长度是a.length(),其中最高位置是a.length(),但你指的是匹配大小写之外的那个角色。 char y = a.charAt(i+1);

因此,当您进行迭代时,您的逻辑应该如下所示进行更改。

while (i<(a.length() -1))//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++;
}

请参阅我仅循环到最后一个字符while (i< (a.length() -1 ))之前的字符。

答案 1 :(得分:0)

我会稍微挑选一下,因为我不能单独留下。

for( i = 1 ; i < a.length() ; i++ )
    if( a.charAt(i) > a.charAt(i-1) )
        return false ;
return true ;

这里,我们不是担心超越字符串的远端,而是开始循环一个字符,并通过查看前一个字符的肩膀来执行每个测试。我也返回布尔值而不是字符串;如果你真的想要一个字符串由于某种原因,只需打一些引号。

答案 2 :(得分:0)

  

每当我尝试获得真正的

时,我都会越过范围错误

问题出在这里:

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);
  }