文件是:
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
第一行是书名,下一行是作者。
我只想从文件中读取前五本书和作者,然后将其存储在堆栈中。将前两本书与作者一起添加到堆栈中,然后删除最后一本书。我该怎么做? 这是我做的
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());
}
最佳答案
假设每本书 + 作者都在文件的一行中:
要只阅读前五本书,请使用 for 循环
而不是 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());
}
}
然后清除堆栈中的最后一项:
readingList.pop() = null;
因为我有很多时间。这是相同的函数,但最大书籍数量可变:
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());
}
}
关于java - 只从文件中读取一些字符串并将其存储在java中的堆栈中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32858840/