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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top