0

I am trying to make a text adventure in java gui. I have just started this project and I am trying to get the game to realize when a player types "get" + the item. I want it to take the item out of the current ArrayList it is in and add it to the inventory screen I have made. Currently everything works correctly except for when I type a different command like "look". All it should show then is the room description, but it hows the description and picks up the item and puts it in the inventory screen. Then I get this error.

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at Gamegui.processCommands(Gamegui.java:327)
at Gamegui.playerInputKeyPressed(Gamegui.java:359)
at Gamegui.access$200(Gamegui.java:14)
at Gamegui$3.keyPressed(Gamegui.java:171)
at java.awt.Component.processKeyEvent(Component.java:6483)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2829)
at java.awt.Component.processEvent(Component.java:6302)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
at java.awt.Component.dispatchEventImpl(Component.java:4752)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 10 seconds)

Here is my relative code.

processCommands

public void processCommands(String input)
{
    ArrayList<String> items = currentRoom.getItems();

    if(input.equals("look"))
    {
        gameScreen.append("\n" + currentRoom.getDescription());
    }
    for(String item : items)
    {
        if(input.equals("get " + item));
        {
            items.remove(item);
            gameScreen.append("\nYou pick up the " + item);
            invScreen.append("\n" + item);
            playerInput.setText("");
        }
    }
}

playerInputKeyPressed

private void playerInputKeyPressed(java.awt.event.KeyEvent evt) {                                       
    if(evt.getKeyCode() == evt.VK_ENTER)
    {
        getInput();
        if(validCommands(inputText)) 
        {
            processCommands(inputText);
        }
        else
        {
            gameScreen.append("\nI don't Understand");
        }

        playerInput.setText("");

        try
        {
            JScrollBar sb = gameScreenPlace.getVerticalScrollBar();
            sb.setValue(sb.getMaximum());
        }
        catch(java.lang.NullPointerException err)
        {

        }
    }
}                   

Can someone please help me fix this?

jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

1

Your removing items while iterating.

   for(String item : items)
    {
        if(input.equals("get " + item));
        {
            items.remove(item);
            gameScreen.append("\nYou pick up the " + item);
            invScreen.append("\n" + item);
            playerInput.setText("");
        }
    }

Use an Iterator and call remove for this manner.

Iterator<String> iter = items.iterator();

while (iter.hasNext()) {
    String str = iter.next();

   if(input.equals("get " + str))
        {
        iter.remove();
        gameScreen.append("\nYou pick up the " + str);
        invScreen.append("\n" + str);
        playerInput.setText("");
        }

}

For reference.

Also remove semicolon to this line.

`if(input.equals("get " + str))`
Ken de Guzman
  • 2,790
  • 1
  • 19
  • 33
  • Well I do not get the error doing it this way, but If I type "get" without the item it goes to my inv screen. And if i type look, it displays the room description, and puts the item in the inv screen. I cannot figure out why – Zach Sutherland Sep 14 '14 at 02:20
  • No problem. You can accept this answer by clicking the check in my answer to resolve this problem. Also, you can upvote or downvote an answer if you like them or not. – Ken de Guzman Sep 14 '14 at 02:49
  • Thanks for that advice as well. I'm pretty new to this. – Zach Sutherland Sep 14 '14 at 03:09