【发布时间】:2022-01-22 07:23:07
【问题描述】:
ActionListener 当builtwordcheck 长度超过 2 个字母时会出错。 ActionListener 按下时的目的是打印列表(单词列表数组文件)中匹配单词(builtwordcheck)并且比该单词长的所有单词。我不确定是否需要关于如何构建单词的进一步上下文,但只是通过单击 String word = word + [letter]
示例:
cat 应该打印出猫,毛毛虫,...
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:1512)
at MainGame$HintAL.actionPerformed(MainGame.java:117)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
class HintAL implements ActionListener{
String Arrayword;
boolean match;
@Override
public void actionPerformed(ActionEvent e) {
match = true;
Arrayword = null;
for (int i = 0; i < wordlist.size(); i++) {
Arrayword = wordlist.get(i);
for (int j = 0; j < builtwordcheck.length(); j++) {
if (Arrayword.charAt(j) != builtwordcheck.charAt(j) || Arrayword.length() <= builtwordcheck.length() /* add condition for having reached the end of the array word (.length) */){
match = false;
}
}
if (match == true){
System.out.println(Arrayword);
}
match = true;
}
}
}
【问题讨论】:
-
是
Arrayword.length() == builtwordcheck.length()? -
我不确定我是否理解正确。但并非总是如此,实际上程序应该在第一个 if 中避免这种情况,例如 `Arrayword.length()
-
在我看来,它们有不同的长度。在没有完整的相关代码sn-p知识的情况下,这是我可以做的一个疯狂的猜测,这可能会导致StringIndexOutOfBoundsException。而且,本题与“swing”无关,可以去掉swing标签。
标签: java arrays string arraylist cycle