ActionListenerbuiltwordcheck當長度超過 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;
}
}
}
uj5u.com熱心網友回復:
您的代碼的以下行正在拋出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——是使用流。
class HintAL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
wordlist.stream()
.filter(s -> s.contains(builtwordcheck))
.forEach(System.out::println);
}
}
請注意,上述代碼的最后一行使用了方法參考(Java 8 中也添加了該方法參考)。
另請注意,建議遵守Java 命名約定。
另外,我認為向您推薦Java by Comparison這本書是合適的
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/418758.html
標籤:
