1

我有一个外部文本文件,它是:

-To Kill a Mockingbird by Harper Lee.
-The Great Gatsby by Scott Fitzgerald.
-Hamlet by William Shakespeare.
-Then Catch in the Rye by J.D Salinger.
-One Hundred Years of Solitude by Gabriel Garcia Marquez.
-The Hobbit by J.R.R Tolkien.
-Moby Dick by Herman Melville.
-A Tale of two Cities by Charles Dickens.
-Don Quixoteby Miguel de Cervantes.
-Love in the Time of Cholera by Gabriel Garcia Marquez.
-Of Mice and Men by John Steinbeck.
-Fahrenheit 451 by Ray Bradbury.
-Stranger in a Strange Land by Robert Heinlein.
-Siddartha by Herman Heese.
-Atlas Shrugged by Ayn Rand.
-The Count of Monte Cristo by Alexandre Dumas.
-The Iliad by Homer.
-The Odyssey by Homer.
-A Wrinkle in Time by Madeleine L'Engle.
-Inferno by Dante Alighieri.
-Paradise Lost by John Milton.
-Alice's Adventures in Wonderland by Lewis Carroll.
-War and Peace by Leo Tolstoy.
-Frankenstein by Mary Shelley.
-Romeo and Juliet by William Shakespeare.
-Exodus by Leon Uris.
-1984 by George Orwell.

我想要做的是拆分每一行的字符串并将它们存储在一个数组列表中我只是不知道为什么它在读取这个文本文件时从第一行跳转到第三行:我的代码:

bookSearch = new Scanner(new FileInputStream("src/booksNames.txt")).useDelimiter(" by ");
            books = new ArrayList<Books>();
            String storeName = "";
            String storeAuthor = "";

            while(bookSearch.hasNextLine())
            {

                storeName = bookSearch.next().split("by")[0];
                storeAuthor = bookSearch.next().split("(by)|(\\.)")[0];

                bookSearch.nextLine();              

                info = new Books(storeName, storeAuthor);
                books.add(info);
            }

我得到的是哈珀李的《杀死一只知更鸟》,然后它跳到威廉莎士比亚的《哈姆雷特》!它只是一直忽略第二,第四,第六行等......任何帮助将不胜感激!

每个标题和作者都是单独的一行!

4

6 回答 6

3

您在 loop.bookSearch.nextLine() 中调用了 bookSearch.next() 两次,每次都会跳到下一个对象。

while(bookSearch.hasNextLine())
            {

                storeName = bookSearch.next().split("by")[0]; 
                storeAuthor = bookSearch.next().split("(by)|(\\.)")[0]; // The error lies here, bookSearch.next() skips to the next object every time

                bookSearch.nextLine();              

                info = new Books(storeName, storeAuthor);
                books.add(info);
            }

正确实施:

while(bookSearch.hasNextLine())
            {
                String bookString = bookSearch.nextLine();
                storeName = bookString.split("by")[0];
                storeAuthor = bookString.split("by")[1];

                info = new Books(storeName, storeAuthor);
                books.add(info);
            }
于 2012-12-10T07:19:54.720 回答
2

发生这种情况是因为您在循环中调用bookSearch.next()了两次while

于 2012-12-10T07:20:08.527 回答
1

bookSearch.next()在循环内调用了两次,bookSearch.nextLine()一次调用了。

从此扫描器中查找并返回下一个完整的令牌。一个完整的标记前后是匹配分隔符模式的输入。此方法可能会在等待输入扫描时阻塞,即使先前调用 hasNext() 返回 true。

while(bookSearch.hasNextLine())
{
      String[] book = bookSearch.next();
      storeName = book.split("by")[0];
      storeAuthor = book.split("(by)|(\\.)")[1];
      info = new Books(storeName, storeAuthor);
      books.add(info);
}
于 2012-12-10T07:20:05.113 回答
0

使用这样的东西

try{
        FileReader fr=new FileReader("Test.txt");
        BufferedReader br=new BufferedReader(fr);

        while((str=br.readLine()) != null){
        strBuf.append(str);
                //here you can add the str to your arrayList

        }
        }catch(Exception e){

        }

这里 Test.txt 是您的输入文件。此代码段将逐行获取数据

从文本文件。

希望这就是你要找的

于 2012-12-10T07:20:16.110 回答
0

您可以使用以下代码读取文件。您还应该考虑以下几点。

  • 您应该首先检查它是否包含“by”关键字,因为有一行不包含“by”,例如“Don Quixoteby Miguel de Cervantes.”。
  • 您应该使用“ by”关键字之间的空格进行拆分,因为由于“Gatsby”还包含“by”,因此它也会将其拆分并给您错误的结果。

    public void readFile(String fileName) {
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(new File(fileName)));
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
    
        String line;
        while ((line = br.readLine()) != null) {
            if (line.contains(" by ")) {
                String[] arr = line.split(" by ");
                String book = arr[0];
                String author = arr[1];
    
                System.out.println("Book : " + book + " Author : " + author + "\n");
                System.out.println("\n");
            } else {
                System.out.println(line + "\n");
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }
    
于 2012-12-10T07:26:33.633 回答
0

你在那里大约90%。需要注意的一件事是String.split()返回的内容-它会返回一个数组String,您正确地记下了它。但是,如果您为正在阅读的每一行创建数组并以这种方式执行处理,您会得到更好的服务。

另外,你打next()了两次电话。回想一下Scanner.next()实际做了什么——最好创建局部变量来保存 theString或 split String[]

于 2012-12-10T07:31:27.573 回答