First Java program. I'm building a flashcard app for fun / self-learning, and I haven't yet figured out how to flip the card to show the back.
ArrayList (called ready) contains objects of my Flashcard class. One of Flashcard's methods is showFront. In MainProgram.java I'll iterate through the ArrayList, calling this method at each iteration in order to show the front of the flashcard, and then wait for keyboard input in order to decide what to do next:
MainProgram.java:
ArrayList<Flashcard> ready = new ArrayList<Flashcard>();
ArrayList<Flashcard> entireDeck = new ArrayList<Flashcard>();
// Create ten flashcards and add them to the pile "entireDeck"
for(int counter = 0; counter < 10; counter++) {
Flashcard FC = new Flashcard();
entireDeck.add(FC);
}
(entireDeck.get(0)).addData(0,"A little","Un poco");
Flashcard.java:
class Flashcard {
public int cardNumber;
public String word;
public String translation;
// Add data to a flashcard
public void addData(int cardNumber, String word, String translation){
this.cardNumber = cardNumber;
this.word = word;
this.translation = translation;
}
public void showFront(int index) {
System.out.println("Card #:\t\t" + cardNumber);
System.out.println("Word:\t\t" + word + "\n\n");
System.out.println("1) Replay audio\t 2) Flip card\t 3) Skip\n");
Scanner nav = new Scanner(System.in);
int userInput = nav.nextInt();
switch (userInput) {
case 1:
System.out.println("Playing audio");
break;
case 2:
System.out.println("Flipping card");
showBack(**what to put here?**);
break;
case 3:
System.out.println("Next card...");
break;
default:
System.out.println("Invalid entry. Come back soon!\n");
break;
}
}
public void showBack(int index) {
System.out.println("Card #:\t\t" + cardNumber);
System.out.println("Translation:\t\t" + translation + "\n\n");
System.out.println("1) Replay audio\t 2) Flip card\t 3) Skip\n");
}
}
A for loop in MainProgram.java was used to add flashcards to the ready deck - I'm just removing most of my code to focus on the issue I'm having.
The console correctly displays the front of the card:
Console output showing current card:
And of course it shows the next card in sequence because I chose to break out of every switch case until I figure out how to correctly pass the right argument into showBack():
Console output showing next card:
Again, stripping out unnecessary code for clarification / brevity.
Correct me if I'm wrong but I can't take the variable i from the loop in MainProgram.java and use it as the argument in showBack() because i is not a global variable. I think that I just need to find the current iteration of that for loop and use it as showBack's argument in order to show the back of that card.
I do realize that eventually I'll have to deal with the scanner memory leak caused by not using nav.close().
I have a feeling this is such an easy solution. **Edit: **I should be using pointers? How can I pass the index of a for loop as the argument for pthread_create