Updating an index variable in Java

In summary, the conversation is about the difficulty in updating the index variable in a program. The person has tried setting the index to different values, but is still having trouble using it to get the previous and next flashCard in the arrayList. They have been working on it for hours and are asking for hints on getting the next and previous buttons to work with the delete function. The conversation also includes code for a FlashCardList class and a FlashCardController class.
  • #1
Smiles1
8
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
  • #2
Closing thread since it's a duplicate of https://mathhelpboards.com/computer-science-58/using-mvc-delete-element-arraylist-25062.html.
 

1. How do you update an index variable in Java?

To update an index variable in Java, you can use any of the following methods:

  • Increment or decrement the variable using the ++ or -- operators.
  • Assign a new value to the index variable using the = operator.
  • Use a loop control statement such as for, while, or do-while to update the index variable.

2. Why is it important to update an index variable in Java?

Updating an index variable is important in Java to keep track of the current position in a loop or an array. It ensures that the program executes the desired number of times or accesses the correct element in the array.

3. Can an index variable be updated within a loop in Java?

Yes, an index variable can be updated within a loop in Java. This is often necessary to control the flow of the loop and perform different operations based on the current index value.

4. What happens if you do not update the index variable in a loop in Java?

If you do not update the index variable in a loop in Java, the loop will continue to execute indefinitely, resulting in an infinite loop. This can cause the program to crash or consume a lot of memory, leading to performance issues.

5. Is it possible to update an index variable in Java using a conditional statement?

Yes, it is possible to update an index variable in Java using a conditional statement, such as if or switch. This allows you to update the index variable based on certain conditions, making your code more flexible and efficient.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
834
  • Programming and Computer Science
Replies
2
Views
635
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top