在搜索超过 2 个匹配字母的单词时尝试比较数组列表中的匹配单词时出错
Error when trying to compare matching words from an arraylist when searching through words with more than 2 matching letters
当 builtwordcheck
长度超过 2 个字母时,ActionListener
给出错误。按下 ActionListener
的要点是打印出列表(单词列表数组文件)中与单词 (builtwordcheck
) 匹配且比该单词长的所有单词。我不确定是否需要关于这个词是如何构建的更多上下文,但只是通过点击 String word = word + [letter]
一个一个地添加它的字母
示例:
cat 应该打印出 cats, caterpillar, ...
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.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue.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.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue.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;
}
}
}
您的代码的以下行抛出 StringIndexOutOfBoundsException
。
if (Arrayword.charAt(j) != builtwordcheck.charAt(j) || Arrayword.length() <= builtwordcheck.length() /* add condition for having reached the end of the array word (.length) */){
这是因为 j
的值 而不是 小于 Arrayword
的长度。
现有代码的最简单修复方法是简单地交换 if
条件中的顺序,即
if (Arrayword.length() <= builtwordcheck.length() || Arrayword.charAt(j) != builtwordcheck.charAt(j)) {
更好的解决方案是调用方法 contains。
if (Arrayword.contains(builtwordcheck)) {
一个更好的解决方案——如果你至少使用 Java 8——是使用 streams.
class HintAL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
wordlist.stream()
.filter(s -> s.contains(builtwordcheck))
.forEach(System.out::println);
}
}
注意上面代码的最后一行使用了method references(也是在Java8中添加的)
另请注意,建议遵守Java naming conventions。
另外,我觉得推荐给你这本书比较合适Java by Comparison
builtwordcheck
长度超过 2 个字母时,ActionListener
给出错误。按下 ActionListener
的要点是打印出列表(单词列表数组文件)中与单词 (builtwordcheck
) 匹配且比该单词长的所有单词。我不确定是否需要关于这个词是如何构建的更多上下文,但只是通过点击 String word = word + [letter]
示例:
cat 应该打印出 cats, caterpillar, ...
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.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue.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.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue.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;
}
}
}
您的代码的以下行抛出 StringIndexOutOfBoundsException
。
if (Arrayword.charAt(j) != builtwordcheck.charAt(j) || Arrayword.length() <= builtwordcheck.length() /* add condition for having reached the end of the array word (.length) */){
这是因为 j
的值 而不是 小于 Arrayword
的长度。
现有代码的最简单修复方法是简单地交换 if
条件中的顺序,即
if (Arrayword.length() <= builtwordcheck.length() || Arrayword.charAt(j) != builtwordcheck.charAt(j)) {
更好的解决方案是调用方法 contains。
if (Arrayword.contains(builtwordcheck)) {
一个更好的解决方案——如果你至少使用 Java 8——是使用 streams.
class HintAL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
wordlist.stream()
.filter(s -> s.contains(builtwordcheck))
.forEach(System.out::println);
}
}
注意上面代码的最后一行使用了method references(也是在Java8中添加的)
另请注意,建议遵守Java naming conventions。
另外,我觉得推荐给你这本书比较合适Java by Comparison