string - Error:Exception in thread "main" java.lang.StringIndexOutOfBoundsException -


i have been trying execute program many find bit useless. nevertheless have been getting error:exception in thread "main" java.lang.stringindexoutofboundsexception while executing.this program find number of vowels,consonants,special characters etc. , got error.please me. error , how remove code .thanks in advance.

import java.io.*; public class numberof { public static void main(string args[])throws ioexception{ bufferedreader br=new bufferedreader(new inputstreamreader(system.in)); system.out.println("enter string"); string str=br.readline(); int vowel=0,consonant=0,upcase=0,locase=0,special=0,dig=0; int l=str.length(); for(int in=1;in<=l;in++){         //this being displayed error     char c=str.charat(in);            //this error line.     if(c>=65||c<=90||c>=97||c<=123){         if(c=='a'||c=='a'||c=='e'||c=='e'||c=='o'||c=='o'|c=='u'||c=='u'){             vowel++;             if(c>=65 && c<=90){                 upcase++;                 }             else{                 locase++;               }         }         else{             consonant++;             if(c>=65 && c<=90){                 upcase++;             }             else{                 locase++;                      }                 }              }     else if(c>=48 && c<=57){         dig++;         }     else{         special++;     }      }     system.out.println(upcase+" "+locase+" "+vowel+" "+consonant+" "+dig+" "+special); }    } 

for(int in=1;in<=l;in++) 

should be

for(int in=0;in<l;in++) 

array index starts 0 (in =0 assuming want first element)

edit:

l length of string[], let 5 split a[0], a[1], a[2], a[3], a[4].

if observe, can start 0 (or) 1 (or) 2, max can go upto a[4] only, when use in <=, loop check until a[5] throws indexoutofbounds exception.


Comments