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?