Reputation: 39
I'm building a relativley simple stock system to account for books I own in a file, however I'm having trouble iterating the inputs.
Here are my imports:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
and the globals:
private static File myFile = new File("libraryList.txt");
private static ArrayList<String> bookTitles = new ArrayList<String>();
private static Scanner inputScanner = new Scanner(System.in);
So the array "bookTitles" stores the title, author, ISBN and genre of a book - all of which have inputs that work. I can input all of the information for one book - and it'll be written successfully.
The main issue comes when attempting to write a second book in.
My "add another book" loop works, however it won't write any further than just the first book entered.
System.out.println("Would you like to add another book? Y/N");
String addAnother = inputScanner.nextLine(); //assigns the answer for addAnother to the variable
if (addAnother.equals("N")) {
addNewBook = false; //if they don't want to add another, end the loop.
WriteToFile();
}
same goes for the file writer - it all works fine, but only for the first iteration.
public static void WriteToFile() {
try {
FileWriter myWriter = new FileWriter(myFile.getName(), true); //True means append to file contents, False means overwrite
System.out.println("This is the contents of the file:");
myWriter.write(""); //makes a space between each book
myWriter.write(bookTitles.get(0)+(" ")); // adds the current list to the file
myWriter.write(bookTitles.get(1)+(" "));
myWriter.write(bookTitles.get(2)+(" "));
myWriter.write(bookTitles.get(3)+("\n\n"));
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
I'm aware that the issue stems from me only accessing units 0-3 from the array, however I can't come up with an iteration that works properly with the rest of my code - everything I've attempted thus far has just broken.
Here's the pastebin link for the full code:
Upvotes: 1
Views: 610
Reputation: 6549
You have identified the issue correctly, in your WriteToFile
method you are only adding a single book from your bookTitles
list to the file, more precisely you are only adding the first book that was input in the current run.
You would have to change that code into a loop that iterates over the whole bookTitles
list. Since you save all the data sequentially into the List, and since you know that you have exactly 4 units of data per book, you can use a for
loop with an index increment of 4
. Then instead of accessing indices at 0, 1, 2, 3
you access indices at i, i+1, i+2, i+3
inside the loop.
Since the index increment is 4
this means that the indices in your loop iterations will be the following:
0, 1, 2, 3 // for the 1st book
4, 5, 6, 7 // for the 2nd book
// ...
size-4, size-4 + 1, size-4 + 2, size-4 + 3 // for the last book
In the original code this then becomes
public static void WriteToFile() {
try {
FileWriter myWriter = new FileWriter(myFile.getName(), true); //True means append to file contents, False means overwrite
// adds the current list to the file
for (int i = 0; i < bookTitles.size(); i += 4) {
myWriter.write(bookTitles.get(i)+(" "));
myWriter.write(bookTitles.get(i+1)+(" "));
myWriter.write(bookTitles.get(i+2)+(" "));
myWriter.write(bookTitles.get(i+3)+("\n\n"));
}
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
Upvotes: 1