简体   繁体   English

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

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

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: 假设每个Book + Author都在你的文件的一行中:

to read only the first five books use a for-loop instead of while : 只读前五本书使用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());
                                        }
                            }

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());
                                        }
                            }

You might want to check for exceptions, in case of a bad file 如果文件不正确,您可能需要检查异常

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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM