快速提问。我在一个程序中有这段代码:
input = JOptionPane.showInputDialog("Enter any word below")
int i = 0;
for (int j = 0; j <= input.length(); j++)
{
System.out.print(input.charAt(i));
System.out.print(" "); //don't ask about this.
i++;
}
Input
是用户输入的内容。i
是一个整数,其值为0。
运行代码时出现以下错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(Unknown Source)
at program.main(program.java:15)
如果我把 charAt
中的 int
改成0而不是 i
,则不会产生错误...
应该怎么解决?问题出在哪里?