Java Updating an index variable in Java

AI Thread 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top