简体   繁体   中英

why i am geeting Index Out Of Bounds Exception error

Here,I'm trying to print next consonant letter, suppose if i give input as java outpt should look as kava. As j and v are consonant therefore it should print next letter if it is a consonant

Enter any string = java bjcException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 at java.lang.String.charAt(Unknown Source) at Consonants.main(Consonants.java:30)

    String consonants = "BCDFGHJKLMNPQRSTVWXYZ".toLowerCase();

    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter any string = ");
    String inputString = bf.readLine().toLowerCase();

    for(int i=0; i < inputString.length(); i++){
        inputChar = inputString.charAt(i);
        //int asciiInput = (int) inputChar; 
        String str1 = inputString.charAt(i) + "";
        int asciiInput = (int) str1.charAt(i);

        for(int j = 0; j < consonants.length(); j++){
         stringChar = consonants.charAt(j);
         System.out.print(stringChar);
         //int asciiConsonants = (int) stringChar;
         String str2 = consonants.charAt(j) + "";
         int asciiConsonants = (int) str2.charAt(j);

         if( !str1.equals(str2) ){
             nextChar = (char) asciiInput;
             System.out.print(nextChar);
         } else if ( str1.equals(str2) ) {
             nextChar = (char) (asciiConsonants + 1);
             System.out.print(nextChar);     
         }

        }
    }   

str2 will have only 1 character becaue consonants.charAt(j) will get 1 character and, "" contains 0 characters.

On the other hand, consonants contains more than 1 character. Therefore, j will become 1 and str2.charAt(j); will emit the error.

You may want (int) str1.charAt(0) and (int) str2.charAt(0) instead of (int) str1.charAt(i) and (int) str2.charAt(j) .

The reason you're getting an error is because str1 and str2 are single-character strings, so charAt() will be out of bounds unless i and j are 0.

Also, if you'll forgive me, I've made some enhancements to your code:

String vowels = "aeiou";
String inputString = bf.readLine().toLowerCase();
for (int i = 0; i < inputString.length(); i++) {
    char c = inputString.charAt(i);
    boolean isVowel = false;
    for (int j = 0; j < vowels.length(); j++) {
        if (vowels.charAt(j) == c) {
            isVowel = true;
            break;
        }
    }
    System.out.print(isVowel ? c : (char)(c + 1));
}

The following code is what's causing your exception:

    String str1 = inputString.charAt(i) + "";
    int asciiInput = (int) str1.charAt(i); 

str1 only has one character, on the second iteration you are asking for the second non-existent character. you'll only work when the index is 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM