JAVA:需要帮助读取数据文件吗

JAVA:需要帮助读取数据文件吗,java,Java,我正在读取一个文本文件Book.txt,并尝试创建一个Book对象,其构造如下: public Book(Person author, String title, String isbn, int year, BookGenre genre){ this.author = author; this.title = title; this.isbn = isbn; this.year = year; this.genr

我正在读取一个文本文件Book.txt,并尝试创建一个Book对象,其构造如下:

public Book(Person author, String title, String isbn, int year, BookGenre genre){
        this.author = author;
        this.title = title;
        this.isbn = isbn;
        this.year = year;
        this.genre = genre;
    }
以下是输入文件的示例:

   Stieg Larsson 0 "The Girl with the Dragon Tattoo" 0307269752 2001 mystery
    Brandon Stanton 0 "Humans of New York" 1250038820 2013 travel
    Oscar Wilde 0 "The Importance of Being Earnest" 158049580X 1895  comic
    Douglas Hofstadter 0 "Gödel, Escher, Bach: An Eternal Golden Braid" 0465026567 1979 science
我不确定作者姓名后的0可以提供什么用途,所以我可能会删除它们,除非你们知道如何使用它们

以下是我在文件中读取的代码:

else if(name.equals("Book.txt")){
        ArrayList<Book> p = new ArrayList<Book>();
        BufferedReader br = new BufferedReader(new FileReader("Book.txt"));
        String line = br.readLine();
        while(line != null)
        {
            String [] tokens = line.split("\\s+");
            p.add(new Book(new Person(tokens[0], tokens[1], Integer.parseInt(tokens[2])), tokens[3], tokens[4], Integer.parseInt(tokens[5]), tokens[6]));
            line = br.readLine();
        }
        br.close();
        }

所以我很确定我只是看错了文件。有人看到任何可以更改以使其更好地工作的内容吗?

首先不要看到代码是如何编译的,因为您希望将
书籍类型
作为
书籍
构造函数的最后一个参数,并向其传递
字符串
。但是,既然这不是所描述的问题,那么让我们假设
bookgreen
类有一个简单的构造函数,它接受
字符串来修复问题,并且您实际上最终调用了它

然而,正如BalusC所指出的,您的问题是您希望标题仅为一个单词,而在示例数据中并非如此

如果您真的想通过按单词拆分文本并对其进行解析来实现这一点,那么请继续使用这种逻辑(不,这不是一个完整的解决方案,因为这需要更多的行,但应该有足够的提示让您了解其余部分):


你在空间上分裂。“女孩”和“有”之间的角色也是一个空间。但是你的代码不知怎么地假设它不是。
Exception in thread "main" java.lang.NumberFormatException: For input string: "with"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:59)
        at java.lang.Integer.parseInt(Integer.java:460)
        at java.lang.Integer.parseInt(Integer.java:510)
        at SortingImplementation.main(SortingImplementation.java:43)
private Book parseBookFromLine(String line) throws Exception {
  String [] tokens = line.split("\\s+");
  Person author = new Person(tokens[0], tokens[1], Integer.parseInt(tokens[2]));
  String title = "";
  int nextToken = 3;

  /*
   * Assume next token starts with quotation marks or throw an exception about
   * malformed data.
   */

  if (tokens[nextToken].endsWith("\"")) {
    // Skim away the quotation.
    String temp = tokens[nextToken++];
    title = temp.substring(1, temp.length - 1);
  } else {
    /*
     * Loop and keep updating the title until you find next token with 
     * quotation mark or there are only five tokens left. In the latter case,
     * if no quotation marks found at the end of current token, throw exception
     * again about malformed data.
     */
  }

  return new Book(author, title, tokens[nextToken++],
                  Integer.parseInt(tokens[nextToken++]),
                  new BookGenre(tokens[nextToken++])));
}