Updating an index variable in Java

  • Context: Java 
  • Thread starter Thread starter Smiles1
  • Start date Start date
  • Tags Tags
    Index Java Variable
Click For Summary
SUMMARY

The forum discussion focuses on issues related to updating an index variable in a Java application managing flashcards. The user struggles with correctly incrementing and decrementing the index variable when navigating through an ArrayList of FlashCard objects. Key problems identified include improper use of the increment (index++) and decrement (index--) operators, which lead to incorrect index values during retrieval and deletion of flashcards.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with ArrayList data structure in Java
  • Knowledge of Java event handling and ActionListener interface
  • Basic concepts of object-oriented programming, specifically classes and methods
NEXT STEPS
  • Review Java ArrayList methods and their usage for dynamic data management
  • Learn about Java increment and decrement operators and their effects on variable values
  • Explore Java event handling to improve user interface responsiveness
  • Investigate best practices for managing state in Java applications
USEFUL FOR

Java developers, software engineers, and anyone involved in building interactive applications using the MVC pattern, particularly those working with dynamic data structures like ArrayLists.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
6K
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