Question

I've been at this for a while, but as the title says, I'm trying to create a method that will compare each letter to the letter after it and see if the word is ascending. The method should then return a boolean value. But when it's implemented in my code, it will fail with:

java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)

and multiple other lines of error code. Here's my source code:

public static boolean ascending(String word){
  int i = 0;
  boolean ascend;
  do {
   if (word.charAt(i) <= word.charAt(i+1))
     ascend = false;
   else
     ascend = true;
  } while (i <= word.length());
  i = 0;
  return (ascend);
}

I can't see where I'm going wrong?

Was it helpful?

Solution

condition should be

i < word.length()-1

and the i = 0; at the end should be in side the loop as i++, else it will be infinite loop.

also, you have actually put the reverse check. once you fix ArrayIndexOutOfBoundsException you will return false for ascending string and true otherwise

public static boolean ascending(String word){
    if(word == null || word.length <2) return false;
    int i = 0;
    boolean ascend = false;
    while(i < word.length()-1){
        if (word.charAt(i) <= word.charAt(i+1))
            ascend = true;
        else{
            ascend = false;
            break;
        }
        i++;
    }    
    return (ascend);
 }

OTHER TIPS

In pseudo code:

  1. take the first letter (F) => store it
  2. take the next letter (N) => check against first => stop here if not OK
  3. is there another next letter => stop here if not
  4. update first letter (F) with next letter (N)
  5. repeat starting from 2

Few things needs to be fixed here:

  1. You need to increment "i" to traverse through the word.

  2. Restrict your while loop till (i < word.length() )

  3. You may break at any point when you find ascend is false. No need to continue with the looping.

I would not use a do while because there is an opportunity to run this on an empty string causing it to crash. I would instead use a regular while and at the beginning test for while (i < word.length()-1). You never want to test past the end of the string. You always want to check to see for a string length of n that charAt(n-1) < charAt(n). Also I don't see an incrementer to increase the value of i. Loop will never continue on to the next letter and will run forever.

public static boolean ascending(String word){
int i = 0;
boolean ascend;
while (i < word.length()-1)
{
if (word.charAt(i) <= word.charAt(i+1))
ascend = false;
else
ascend = true;
i++;
}
i = 0;
return (ascend);

}

Do not use do-while loop. Even if you do you need to run some checks beforehand to make sure your word has length more than 1 otherwise your code will raise an IndexOutOfBounds Exception. If you insist on using it, change your condition to i

Here is a working code example:

public boolean isAscending(String word){
    if (word==null || word.length==0){
        return false;
    }
    for (int i=1; i<word.length(); i++){
        if (word.charAt(i) < word.charAt(i-1))
            return false;
    }

    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top