the file is:
The Lord Of the Rings
J.R.R. Tolkein
Great Expectations
Charles Dickens
Green Eggs and Ham
Dr. Seuss
Tom Sawyer
Mark Twain
Moby Dick
Herman Melville
The Three Musketeers
Alexander Dumas
The Hunger Games
Suzanne Collins
1984
George Orwell
Gone With the Wind
Margret Mitchell
Life of Pi
Yann Martel
It has the title of the book in first line and in the next line it has author.
I only want to read first five books and authors from the file and then store it in a stack. Add the first two books with author in a stack and then remove the last one. How can I do it? This is what I did
Stack<Book> readingList = new Stack<>();
File myFile = new File("books.txt");
Scanner input = new Scanner(myFile);
int i = 0;
while (input.hasNext()) {
readingList.push(new Book(input.nextLine(), input.nextLine()));
System.out.println("Adding: " + readingList.lastElement().getInfo());
readingList.push(new Book(input.nextLine(), input.nextLine()));
System.out.println("Adding: " + readingList.lastElement().getInfo());
System.out.println("Reading: " + readingList.pop().getInfo());
}
Asumming that every Book + Author is in one line of your file:
to read only the first five books use a for-loop instead of while:
Stack<Book> readingList = new Stack<>();
File myFile = new File("books.txt");
Scanner input = new Scanner(myFile);
int i = 0;
int counter = 0;
for (int numberOfBookToRead = 0; numberOfBookToRead < 2;numberOfBookToRead++) {
try {
if(readingList.hasNextLine() && counter <= 4){ // provides that only 4 books are pushed while the iteration is going, and also works
readingList.push(new Book(input.nextLine(), input.nextLine()));
counter += 1;
System.out.println("Adding: " + readingList.lastElement().getInfo());
readingList.push(new Book(input.nextLine(), input.nextLine()));
counter +=1;
System.out.println("Adding: " + readingList.lastElement().getInfo());
System.out.println("Reading: " + readingList.pop().getInfo());
readingList.pop() = null;
}catch(Exception e){
e.printStackTrace();
}
} else if(readingList.hasNextLine){
readingList.push(new Book(input.nextLine(), input.nextLine()));
System.out.println("Adding: " + readingList.lastElement().getInfo());
}
}
afterwards to clear the last item from stack:
readingList.pop() = null;
Because i got a lot of time. Here is the same function but with a variable number of maximum books:
Stack<Book> readingList = new Stack<>();
File myFile = new File("books.txt");
Scanner input = new Scanner(myFile);
int i = 0;
int counter = 0;
int maxNumberOfBooks = 10; //could be any number you wish
for (int numberOfBookToRead = 0; numberOfBookToRead < Math.round(maxNumberOfBooks/2)-1;numberOfBookToRead++) {
try {
if(readingList.hasNextLine() && counter <= maxNumberOfBooks-1){
readingList.push(new Book(input.nextLine(), input.nextLine()));
counter += 1;
System.out.println("Adding: " + readingList.lastElement().getInfo());
readingList.push(new Book(input.nextLine(), input.nextLine()));
counter +=1;
System.out.println("Adding: " + readingList.lastElement().getInfo());
System.out.println("Reading: " + readingList.pop().getInfo());
readingList.pop() = null;
}catch(Exception e){
e.printStackTrace();
}
} else if(readingList.hasNextLine){
readingList.push(new Book(input.nextLine(), input.nextLine()));
System.out.println("Adding: " + readingList.lastElement().getInfo());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With