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

标签 java file collections while-loop stack

文件是:

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/

相关文章:

java - 从外部源系统下载 AWS Lambda 的源代码

java - 如何加速Tomcat中spring应用的重启?

php - 尽管文件存在,但 laravel 存储存在返回 false

collections - MVC 6 标签助手和 foreach

java - 列表中子列表中的最大值

c# - 将集合返回为只读

java - 为什么整数数据类型会默默溢出而不是抛出异常

java - 如何在Websphere应用程序服务器中安装端点URL的SSL证书

java - 如何在 if、嵌套 if、else 语句中使用具有两种含义的 boolean 值

javascript - 我如何在javascript中打开任何类型的文件并将其保存为字符串