java中的索引越界异常
异常消息:java中的索引越界异常,java,arrays,exception,Java,Arrays,Exception,异常消息: Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2 Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2 下面是我用来突出显示JTextPane中特
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
下面是我用来突出显示JTextPane中特定单词的代码。我的目标是创建一个简单的语法突出显示编辑器,我对它进行了彻底的搜索,并找到了许多有趣的解决方案,但我想为它编写自己的代码,现在我陷入了IndexOutOfBoundsException
每当按下第三个键时,我的编辑器就会给出这个异常,这意味着每当JTextPane中写入两个字母时
如果代码不容易理解,我深表歉意,因为我不熟悉学习惯例
我知道这是一个非常琐碎的问题,但任何形式的帮助都是相当大的。
谢谢各位:
[更新]代码的第一部分处理jTextPane2KeyTyped事件
String[] words = new String[] {"if","else","for"};
//words is the list for words to change color
StyledDocument doc = jTextPane2.getStyledDocument();
Style style=doc.addStyle("Red_Colour", null);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setForeground(common,Color.BLACK);
String temp = jTextPane2.getText();
//temp holds the string value of the text present in the jTextPane2
int check=0;
for(int i=0;i<temp.length();i++){
for(int j=0;j<words.length;j++){
if(charLeft(temp,words,i,j)){
if(temp.length()>=words[j].length())
for(int k=0;k<words[j].length();k++){
if(temp.charAt(i+k)==words[j].charAt(k))check++;
}
//else{break;}
if(check==words[j].length()){
doc.setCharacterAttributes(i,words[j].length(),style, false);
}
}
}
}
异常的回溯
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.charAt(String.java:658)
at NewJFrame.jTextPane2KeyTyped(NewJFrame.java:164)
at NewJFrame.access$000(NewJFrame.java:24)
at NewJFrame$1.keyTyped(NewJFrame.java:59)
at java.awt.Component.processKeyEvent(Component.java:6460)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2828)
at java.awt.Component.processEvent(Component.java:6282)
...
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.charAt(String.java:658)
at NewJFrame.jTextPane2KeyTyped(NewJFrame.java:164)
at NewJFrame.access$000(NewJFrame.java:24)
at NewJFrame$1.keyTyped(NewJFrame.java:59)
at java.awt.Component.processKeyEvent(Component.java:6460)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2828)
...
实际上,在第三个循环中,您也在增加j,而不是k: 自发的猜测是您应该将其更改为以下内容,以避免超过最大长度:
if(temp.length()>=words[j].length())for(int k=0;k<words[j].length();k++)
我认为这一行可能是您的问题这是您发布的第二个代码块的第3行:
if(temp.length()>=words[j].length())for(int k=0;k<words[j].length();j++){if(temp.charAt(i+k)==words[j].charAt(k))check++;}
你可能想说:
if(temp.charAt(i)==words[j].charAt(k))check++;
修正!问题是我没有将count的值改回零。另一个问题是charLeft中的条件运算符,它是
if(temp.length()<words[j].length())return true;
请格式化您的代码。这真的不可读。显示异常消息和回溯的前几行会很有帮助,而且通常是让任何人都能看到您的问题的一项要求。@ZouZou我尽了最大努力进一步格式化it@HotLicks我添加了异常消息。@HotLicks我不知道什么是回溯。谢谢你通知我,但是我仍然得到同样的例外。@TahaRushain在哪一行?:先生,我不知道它在哪里超出了范围,只是每当在jTextPane2中键入超过2个字母时,控制台就会出现异常,说字符串索引超出范围:2,程序就会冻结。
for(int k=0;k<words[j].length();j++)
for(int k=0;k<words[j].length();k++)
if(temp.charAt(i+k)==words[j].charAt(k))check++;
if(temp.charAt(i)==words[j].charAt(k))check++;
if(temp.length()<words[j].length())return true;
if(temp.length()>=words[j].length())return true;