Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exception in thread "main" java.lang.StringIndexOutOfBoundsException

807597Feb 8 2005 — edited Feb 8 2005
Exception in thread "main" java.lang.StringIndexOutOfBoundsException

:String index out of range: 6

thats the eror i receive when i run this code:
import javax.swing.JOptionPane;
import java.awt.*;	

import javax.swing.JOptionPane;
 
public class d5
{
   static String answer;
   public static void main (String[] lisaLisaAndCultJam)
   {
      String ArrayOne[] = new String[20];//god only knows why you are doing this
      answer = JOptionPane.showInputDialog(null, "Feel the rhythym of the night");
      //what you were doing with your array of strings at this point, I'll never know!
      System.out.println("The User Input Was:"+answer);
   
   
   
   int WordCount=0;
   String x=".";
   	for (int j=0;j<ArrayOne.length;j++)
   	{
   	   
   	     if (answer.charAt(j)==(' '))
   	         {
   	         	System.out.print("true");
   	         	WordCount++;
   	         }
         else if
         (answer.charAt(j)==('.'))
   	         {
   	         	System.out.print("true");
   	         	WordCount++;
   	         }
         else if
         (answer.charAt(j)==(','))
   	         {
   	         	System.out.print("true");
   	         	WordCount++;
   	         }
     }

System.out.println(WordCount+"Words");

}
}
I think it might have to do with te index i set for my array wen it was initialized or wen the program reaches the end of the user input. What do u think the problem is?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 8 2005
Added on Feb 8 2005
7 comments
1,470 views