简体   繁体   English

使用扫描仪读取txt.file

[英]Using a Scanner to read a txt.file

I am trying to read a .txt file using the built in Scanner class in java 8, however I am getting mixed results with essentially the same lines of code. 我正在尝试使用Java 8中的内置Scanner类读取.txt文件,但是使用基本上相同的代码行却得到了混合结果。 A little background about my .txt files: they both consist of 1000 line's of different data (One is about a Book and it's features, and the other is about the Patrons). 关于我的.txt文件的一些背景知识:它们都由1000行不同数据组成(一个是关于书本及其功能的,另一个是关于赞助人的)。 When I go out to print my list however, the Patron.txt file prints out all 1000 lines while my Books.txt only prints out a maximum of 483 lines. 但是,当我外出打印清单时,Patron.txt文件会打印全部1000行,而我的Books.txt最多只能打印483行。 Below I showed what line 483 of my .txt file looks like and it looks similar to all the other ones. 下面,我显示了.txt文件的483行,它看起来与所有其他文件相似。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class LibManager {
    private ArrayList<Book> bookList = new ArrayList<Book>();
    private ArrayList<Patron> patronList = new ArrayList<Patron>();
    private ArrayList<Loan> loanList = new ArrayList<Loan>();
    private String[] menuOptions;

    public static void main(String[] args) {
        LibManager lm = new LibManager();
        lm.execute();
    }

    public LibManager() {
        bookList = readBooks("Resources/books.txt");
        patronList = readPatrons("Resources/patrons.txt");
        loanList = readLoans("Resources/loans.txt");

        menuOptions = new String[] { "Add Book", "Add Patron", "List Books", "List Patrons", "List By Author",
                "List By Year", "Lend Book", "Return Book", "Show Borrower", "Show Borrowed Books", "Remove Book", "Remove Patron",
                "Show Overdue Books", "Exit" };
    }

    private void execute() {

        while (true) {
            int opt = getMenuOption();
            switch (opt) {
            case 1:
                addBook();
                break;
            case 2:
                addPatron();
                break;
            case 3:
                listBooks();
                break;
            case 4:
                listPatrons();
                break;
            case 5:
                listByAuthor();
                break;
            case 6:
                listByYear();
                break;
            case 7:
                lendBookToPatron();
                break;
            case 8:
                returnBook();
                break;
            case 9:
                showBorrowers();
                break;
            case 10:
                showBorrowedBooks();
                break;
            case 11:
                removeBook();
                break;
            case 12:
                removePatron();
                break;
            case 13:
                showOverdueBooks();
                break;
            case 14:
                exitProgram();
                break;
            default:
                System.out.println("No such option");
            }
        }

    }

    private int getMenuOption() {

        System.out.println("Select one of the following options");
        for (int i = 0; i < menuOptions.length; i++) {
            System.out.println("\t" + (i + 1) + ". " + menuOptions[i]);
        }

        Scanner s = new Scanner(System.in);
        int choice = s.nextInt();

        return choice;
    }

    /* MAKE NO CHANGES ABOVE THIS LINE */
    /* COMPLETE ALL THE CODE STUBS BELOW */

    private void exitProgram() {
        System.out.println("Exiting..");
        System.exit(0);
    }

    private ArrayList<Book> readBooks(String filename) {
         List lines = new ArrayList();
            Scanner s = null;
            File infile = new File(filename);
            try {
                s = new Scanner(infile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            while (s.hasNext()) 
                lines.add(s.nextLine().replaceAll("\\s+",""));
            return (ArrayList<Book>) lines;
    }

    private ArrayList<Patron> readPatrons(String filename) {
        List lines = new ArrayList();
        Scanner s = null;
        File infile = new File(filename);
        try {
            s = new Scanner(infile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        while (s.hasNext()) 
            lines.add(s.nextLine());
        return (ArrayList<Patron>) lines;
    }

Results for readBooks (the last 5 lines): Expected B999. readBooks的结果(最后5行):预期为B999。

B48;Choke;ChuckPalahniuk;1897
B480;Voss;PatrickWhite;1953
B481;TheMidwichCuckoos;JohnWyndham;1864
B482;BlueNoon;GeorgesBataille;1781
B483;HomoFaber;MaxFri

Results for readPatrons (the last 5 lines): Expected P999 readPatrons的结果(最后5行):预期为P999

P995    MURILLO
P996    LUTZ
P997    DUARTE
P998    KIDD
P999    KEY

Looking at my Books.txt file, there is nothing wrong with line 483 查看我的Books.txt文件,第483行没有错

B483    ;   Homo Faber  ;   Max Frisch      ;   1955

UPDATE: The problem still persists, but I am allowed to add another book on top of the list to get it past the 483 mark. 更新:问题仍然存在,但允许我在列表顶部添加另一本书,以使其超过483分。


Used to print out the book list. 用于打印书籍清单。

private void listBooks() {
        for(int i=0;i<bookList.size();i++){
            System.out.println(bookList.get(i));
        }
    }

I some how fixed my problem on my own. 我自己如何解决问题。 So I'm going to post what I did. 所以我要发表我的所作所为。 I imported FileInputStream and added it to my readBooks method. 我导入了FileInputStream并将其添加到我的readBooks方法中。 However, I am still confused why this worked with FileInputStream and not my original method? 但是,我仍然感到困惑,为什么它可以与FileInputStream一起使用而不是与我的原始方法一起使用?

private ArrayList<Book> readBooks(String filename) {
        ArrayList<Book> lines = new ArrayList<>();
        Scanner s = null;
        File infile = new File(filename);
        try{
            FileInputStream fis = new FileInputStream(infile);
            s = new Scanner(fis);
        } catch (FileNotFoundException e){
            e.printStackTrace();
        }
        while(s.hasNextLine())
            lines.add(new Book(s.nextLine()));
        return lines; 

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

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