2012-12-10 159 views
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. 

什么,我试图做的是分裂的字符串的每一行,并将其存储在一个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 ..... 任何帮助将不胜感激!

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

回答

3

你叫bookSearch.next()内loop.bookSearch.nextLine两次()每次跳到下一个对象。

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); 
      } 
+0

现在,我的storeAuthor存储与storeName相同的信息,它是标题!它一直行,但storeAuthor引用标题而不是作者!会发生什么问题? – jv0006

+0

在索引1处使用拆分的字符串。author = bookString.split(“by”)[1];但是,您可能必须删除尾随的“。”。 – Rahul

+0

谢谢你!你已经帮了很多!我还没有弄清楚我的错误是什么!我现在没有想法,我敢打赌这只是一个愚蠢的错误! – jv0006

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); 
} 
+0

bookSearch.next()将返回一个字符串,而不是书籍对象 – Rahul

+0

感谢您的注意... :) –

+0

@乔伊现在,我的storeAuthor存储相同的信息,作为storeName这是标题!它一直行,但storeAuthor引用标题而不是作者!会发生什么问题? – jv0006

2

这是因为你在while循环调用bookSearch.next()两次

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的是你的输入文件。这个代码段会通过线数据

从文本文件读取行。

希望这是你要找的

0

您可以使用下面的代码读取文件。你也应该考虑以下事项。

  • 您应该首先检查它是否包含“by”关键字,因为有一行不包含“by”,如“Don Quixoteby Miguel de Cervantes”。
  • 你应该使用分裂之间的空间“由”关键字,因为自从“盖茨比”,也包含“由”它也将拆分此,给你错误的结果。

    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(); 
         } 
        } 
    } 
    } 
    
+0

读取文件,但不分割线条。 – Makoto

+0

更新了我的代码并添加了拆分代码。 –

0

你约90%存在。其中一个要注意的事情是什么String.split()回报 - 它给你回的String数组,你正确地指出。但是,如果您为阅读的每行创建数组,并且以这种方式执行处理,则会更好。

此外,你打电话next()两次。回想一下Scanner.next()实际上做了什么 - 最好的方法是创建局部变量来保存String或拆分String[]

+0

我只是不知道这个代码有什么问题!我正在用eclipse调试它,我只是没有得到期望的结果!我已经尝试了一切!我敢打赌这是个愚蠢的错误! – jv0006

+0

休息一下,重新阅读这里的答案。 'Scanner.next()'有两个不同的常见主题。 – Makoto