我有一个外部文本文件,它是:
-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);
}
我得到的是哈珀李的《杀死一只知更鸟》,然后它跳到威廉莎士比亚的《哈姆雷特》!它只是一直忽略第二,第四,第六行等......任何帮助将不胜感激!
每个标题和作者都是单独的一行!