我有一個外部的文本文件,該文件是:拆分這個字符串?
-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.
什麼,我試圖做的是分裂的字符串的每一行,並將其存儲在一個ArrayList的 我只是不知道爲什麼它從第一跳行至第三行讀取時,該文本文件: 我的代碼:
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);
}
我所得到的是殺死一隻小八哥由哈珀·李,然後由威廉·莎士比亞跳轉至哈姆雷特!它只是不斷忽略第二,第四,第六行,etcc ..... 任何幫助將不勝感激!
每一個標題和作者是一個獨立的行!
現在,我的storeAuthor存儲與storeName相同的信息,它是標題!它一直行,但storeAuthor引用標題而不是作者!會發生什麼問題? – jv0006
在索引1處使用拆分的字符串。author = bookString.split(「by」)[1];但是,您可能必須刪除尾隨的「。」。 – Rahul
謝謝你!你已經幫了很多!我還沒有弄清楚我的錯誤是什麼!我現在沒有想法,我敢打賭這只是一個愚蠢的錯誤! – jv0006