ActionListener gives error when builtwordcheck length is longer than 2 letters. The point of the ActionListener when pressed is to print out all words in a list (word list array file) that match a word (builtwordcheck) and are longer than that word. I'm not sure if there is needed further context as to how the word is built, but simply it's letters are added one by one through clicks through String word = word [letter]
Example:
cat should print out 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$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;
}
}
}
CodePudding user response:
The following line of your code is throwing StringIndexOutOfBoundsException.
if (Arrayword.charAt(j) != builtwordcheck.charAt(j) || Arrayword.length() <= builtwordcheck.length() /* add condition for having reached the end of the array word (.length) */){
This is because the value of j is not less than the length of Arrayword.
The simplest fix for your existing code is simply to swap the order in the if condition, i.e.
if (Arrayword.length() <= builtwordcheck.length() || Arrayword.charAt(j) != builtwordcheck.charAt(j)) {
A better solution would be to call method contains.
if (Arrayword.contains(builtwordcheck)) {
An even better solution – if you are using at least Java 8 – is to use streams.
class HintAL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
wordlist.stream()
.filter(s -> s.contains(builtwordcheck))
.forEach(System.out::println);
}
}
Note that the last line in the above code uses method references (which were also added in Java 8).
Also note that it is recommended to adhere to Java naming conventions.
Also, I think it is appropriate for me to recommend to you the book Java by Comparison
