ArrayList problem
Hi all,
I have a question. I'm writing a flashcard program, and have a class called Flashcard, which just has two strings for the front and back. The class Deck has an ArrayList which is filled with Flashcards in createdeck(). When I try to save the deck to a text file, I'm getting problems.
When I get the card from the ArrayList, by first declaring a new flashcard and then doing a ArrayList.get(), Netbeans says it found a java object and Flashcard is required. If I try to do it all in one go, it says it can't find the variable back. HEre is the while loop:
Flashcard onecard=new Flashcard(" ", " ");
while (i < decktosave.numcard){
// Netbeans gives error that I have a java object but need flashcard
onecard=decktosave.cards.get(i);
p.println (onecard.front);
// Netbeans says it can't find the variable back
p.println((decktosave.cards.get(i)).back);
i++;
}
The whole Deck and Flashcard classes are shown below:
class Deck {
ArrayList cards=new ArrayList();
int numcard;
public Deck(){
numcard=0;
}
public Deck createdeck(){
String front="test";
String back="test 2";
String dummy=" ";
int iopt=0;
Deck newdeck = new Deck();
boolean test;
do {
System.out.println("Deck creation. 0=quit");
System.out.println("Enter front of Card ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
front= br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read entry!");
System.exit(1);
}
System.out.println("Enter back of card");
// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
back= br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read entry");
System.exit(1);
}
Flashcard onecard = new Flashcard(front, back);
cards.add(onecard);
numcard=numcard+1;
}
while (!front.equalsIgnoreCase("0"));
System.out.println("Do you want to save the deck? 1- yes");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
dummy= br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read entry!");
System.exit(1);
}
iopt=Integer.valueOf(dummy).intValue();
if (iopt==1) newdeck.savedeck(newdeck);
return newdeck;
}
public int savedeck(Deck decktosave){
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object
try
{
// Create a new file output stream
// connected to "myfile.txt"
out = new FileOutputStream("myfile.txt");
// Connect print stream to the output stream
p = new PrintStream( out );
p.println(decktosave.numcard);
int i=0;
Flashcard onecard=new Flashcard(" ", " ");
while (i < decktosave.numcard){
// Netbeans gives error that I have a java object but need flashcard
onecard=decktosave.cards.get(i);
p.println (onecard.front);
// Netbeans says it can't find the variable back
p.println((decktosave.cards.get(i)).back);
i++;
}
p.close();
}
catch (Exception e)
{
System.err.println ("Error writing to file");
}
return 0;
}
public int usedeck(){
return 0;
}
public Deck opendeck(){
Deck newdeck = new Deck();
return newdeck;
}
}
class Flashcard {
String front;
String back;
public Flashcard(String newfront, String newback) {
front=newfront;
back=newback;
}
public static String next() {
String t="Yersina pestis";
return t;
}
}

