Java Updating an index variable in Java

Click For Summary
The discussion centers around issues with updating the index variable in a Java program designed to manage flashcards. The user is struggling to implement functionality for navigating to the previous and next flashcards after deleting one. They have attempted various methods to set the index but are encountering problems with retrieving the correct flashcard. The code provided shows a FlashCardList class that manages an ArrayList of FlashCard objects, with methods for adding, retrieving, and deleting flashcards. Key issues highlighted include incorrect index manipulation, particularly with the use of increment and decrement operators, which may lead to out-of-bounds errors when accessing the ArrayList. The user seeks hints to resolve the navigation and deletion issues effectively.
Smiles1
Messages
7
Reaction score
0
For the life of me, I can't figure out why my program won't update the index variable. I've tried setting the index to 0, and then to -1, and to the size of the arrayList - 1. I'm trying to use the index to get the previous and next flashCard in the arrayList to show up.

I have been working on this for HOURS for the last few days and cannot figure out where or what I'm doing wrong. Please give me hints on getting my next and previous buttons to work with the delete. :-(

Thank you!


FlashCardList
Code:
import java.util.ArrayList;

public class FlashCardList {

    private ArrayList<FlashCard> flashCards;
    private FlashCard flashCard;
    private int index;
    private int position = -1;

    FlashCardList() {
        this.flashCards = new ArrayList();
        this.flashCards.add(new FlashCard("test"));
        
    }

    public void putFlashCard(String textColor) {
        flashCard = new FlashCard(textColor);
        flashCards.add(flashCard);
        System.out.println(flashCards.toString());
        if(flashCards.size() == 1){
        this.index = index + 1;
        }
        else{
            this.index = index++;
        }
    }

    public FlashCard getFlashCard() {
        if (this.index < flashCards.size()) {
            System.out.println("Get current flashCard index " + this.index);
            
            return flashCards.get(index);
        } else {
            this.index = index--;
            System.out.println("Get current flashCard index " + index);
            return flashCards.get(index);
        }
    }

    public FlashCard getNextFlashCard() {
        if (this.index < flashCards.size()) {
            this.index = index++;
            System.out.println("Get Next FlashCard index " + this.index);
            return flashCards.get(this.index);
        } else {
            this.index--;
            System.out.println("Get Next FlashCard index " +  this.index);
            return flashCards.get(index);
        }
    }

    public FlashCard getPreviousFlashCard() {
        if (this.index < flashCards.size()) {
            System.out.println("Get previous flashCard index " + this.index);
            return flashCards.get(index);
        } else {
            this.index = index--;
            System.out.println("Get previous flashCard index " + this.index);
            return flashCards.get(index);
        }
    }

    public int getCurrentIndex(String displayText) {
     
        for (int i = 0; i < flashCards.size(); i++) {

            if (flashCards.get(i).getTextColor().equals(displayText)) {

                position = i;
                System.out.println(position);
                break;

            }

        }
        return position;
    }

    public void deleteFlashCard() {
        this.flashCards.remove(index);
        this.index = index--;
        System.out.println("DELETE");
        System.out.println(flashCards.toString());
    
    }

    public void exitApplication() {
        System.exit(0);
    }

}
FlashCardController

Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class FlashCardController {

    private FlashCardListUI flashCardListUI;
    private FlashCardList flashCardList;

    FlashCardController(FlashCardListUI flashCardListUI, FlashCardList flashCardList) {

        this.flashCardList = new FlashCardList();
        this.flashCardListUI = new FlashCardListUI();
        
        flashCardListUI.setCurrentFlashCard(this.flashCardList.getFlashCard().getTextColor());

        
        class NextFlashCardButtonListener implements ActionListener {

            public void actionPerformed(ActionEvent ae) {
                flashCardListUI.setCurrentFlashCard(flashCardList.getNextFlashCard().getTextColor());
      
            }
        }
        class AddButtonListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                flashCardList.putFlashCard(flashCardListUI.getCurrentFlashCard());

            }
        }

        class BackButtonListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                flashCardListUI.setCurrentFlashCard(flashCardList.getPreviousFlashCard().getTextColor());
                
            }
        }
        
         class DeleteButtonListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                flashCardList.deleteFlashCard();
                flashCardListUI.setCurrentFlashCard(flashCardList.getNextFlashCard().getTextColor());
              
            }

         }
         class EditButtonListener implements ActionListener {
             
             public void actionPerformed(ActionEvent e) {
                 
                 //int i = flashCardList.getCurrentIndex();
                 
                 
                 //System.out.println(i);
            }
         }
         
          class ExitButtonListener implements ActionListener {
             
             public void actionPerformed(ActionEvent e) {
                 flashCardList.exitApplication();
                 
            }
         
         }
          
        flashCardListUI.nextFlashCardButtonListener(new NextFlashCardButtonListener());
        flashCardListUI.addFlashCardButtonListener(new AddButtonListener());
        flashCardListUI.backButtonListener(new BackButtonListener());
        flashCardListUI.deleteButtonListener(new DeleteButtonListener());
        flashCardListUI.exitButtonListener(new ExitButtonListener());
        flashCardListUI.editButtonListener(new EditButtonListener());
        
        
    }

}
 
Technology news on Phys.org
Closing thread since it's a duplicate of https://mathhelpboards.com/computer-science-58/using-mvc-delete-element-arraylist-25062.html.
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
4
Views
2K