【问题标题】:Error when trying to compare matching words from an arraylist when searching through words with more than 2 matching letters在搜索具有超过 2 个匹配字母的单词时尝试比较来自 arraylist 的匹配单词时出错
【发布时间】: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


【解决方案1】:

以下代码行抛出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(Java 8 中也添加了)。

另外注意,建议坚持Java naming conventions。

另外,我觉得向你推荐这本书Java by Comparison是合适的

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    相关资源
    最近更新 更多