仅从文件中读取一些字符串并将其存储在 java 中的堆栈中

reading only some strings from a file and storing it in stack in java

文件是:

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-loop 而不是 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());
                                        }
                            }

您可能想要检查异常情况,以防文件损坏

Stack<Book> readingList = new Stack<>();
File myFile = new File("books.txt");
Scanner input = new Scanner(myFile);

for (int count = 0; count < 5; count++) {

    try {
        readingList.push(new Book(input.nextLine(), input.nextLine()));
        System.out.println("Adding: " + readingList.lastElement().getInfo());
        System.out.println("Reading:  " + readingList.pop().getInfo());
    }
    catch(Exception e) {

        System.out.println(e.getMessage());
    }
}