我有一个外部的文本文件,该文件是:拆分这个字符串?
-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