-2

Not sure where I am going wrong with this, the false statement seems to work fine but I am getting and out of range error whenever i try to obtain true. clarifying, what i want this to do is make sure the ASCII code is increasing with every follows letter, if not return false, is so return 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;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
Consultion
  • 35
  • 2
  • 8
  • Don't link your code, put it in the question (and ideally minimize the code to the smallest possible section) – Foon Aug 29 '15 at 01:02
  • `charAt` doesn't give ASCII codes, it gives UTF-16 code units. Since a codepoint can require one or two code units in UTF-16, you might want to iterate over codepoints instead of code units. You could use [codePointAt and offsetByCodePoints](http://stackoverflow.com/a/1527891/2226988) or [for(int c : string.codePoints().toArray())](http://stackoverflow.com/a/27798012/2226988). Of course, comparing codepoints gives you a lexicographic ordering, which is seldom meaningful to humans. Does anyone really know or care that € < ₹? – Tom Blodget Aug 29 '15 at 03:13

3 Answers3

2

The issue here is that you refer a character which resides beyond the characters of a. See the following part.

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

Here your word length is a.length() where the the highest position is a.length() but you are referring to a character beyond that point where in matching case. char y = a.charAt(i+1);

So your logic should be change as following when you iterating.

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

See I am only looping to the character before last character while (i< (a.length() -1 )).

Eranda
  • 1,439
  • 1
  • 17
  • 30
  • okay I gotcha thanks so much, so to clarify when increasing the value, its gets out of range of the length of a causing bad input to happen? – Consultion Aug 29 '15 at 01:02
0

I'm going to pick on this just a bit more, just because I can't leave well enough alone.

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

Here, instead of worrying about overrunning the far end of the string, we just start the loop one character in, and execute each test by looking over our shoulder at the previous character. I also return the Boolean instead of a string; if you really want a string for some reason, just slap some quotes around them.

zerobandwidth
  • 1,213
  • 11
  • 18
0

I am getting and out of range error whenever i try to obtain true

Problem is here:

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

Try this code:

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