5 Replies Latest reply: Oct 1, 2012 2:59 PM by 939520 RSS

    throwing index out of bound exception- please explain to fix the error

    884510
      import java.util.StringTokenizer;
      import java.lang.String;


      public class tokenize
      {
                String s="I have learned that people will forget what you said, people will forget what you did, but people will never forget how you made them feel. ";
                String s1="Nature always wears the colors of the spirit.";
                
                          public void noofwords()
                          {
                               StringTokenizer str= new StringTokenizer(s," ");
      StringTokenizer str1= new StringTokenizer(s1," ");
                               System.out.println("the no of words in the string s is: "+str.countTokens()+" and the number of words in s1 is: "+str1.countTokens);
                          }
                          
                          public void nooflines()
                          {
                               StringTokenizer str2= new StringTokenizer(s1,".,?");
                               StringTokenizer str3 = new StringTokenizer(s,".,?");
                               System.out.println("the number of lines in string s is: "+str3.countTokens()+" "+"and number of lines in the string s1 is: "+str2.countTokens());
                               
                          }
                          
                          public void noofchars()
                          {
                               System.out.println("no of characters in string s is: "+s.length()+ " " +"and the number of lines in the string s1 is : "+s1.length());
                          }

                          public void noofvowels()
                          {
                               StringTokenizer str4 = new StringTokenizer(s," ");
                               String strpackets;
                               int count =0;
                               if(str4.hasMoreElements())
                               {
                                    strpackets= str4.nextToken();
                                    for( int a=0;a<=strpackets.length();a++)
                                         {
                                         char c= strpackets.charAt(a);
                                              
                                              if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
                                                   {
                                                   count++;
                                                   }                                   
                                         }     
                               }
                               System.out.println("the number of vowels in the sring s is: "+count);
                          }

           public static void main(String[] args)throws StringIndexOutOfBoundsException
      {
                tokenize tze = new tokenize();
                tze.noofwords();
                tze.noofchars();
                tze.nooflines();
                tze.noofvowels();
      }
      }

      Edited by: 881507 on Sep 10, 2012 9:46 PM