2015-12-24 163 views
2

我試圖做一個拼寫檢查器,打開鏈接列表中的.txt文件中的單詞,但hasNextLine()總是返回false。hasNextLine()總是返回false

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.Serializable; 
import java.util.LinkedList; 
import java.util.Scanner; 


public class backEnd { 
    String string; 
    String[] splitted; 

    public backEnd(String s){ 
     string=s; 
    } 
    public void splitter(){ 
     splitted =string.split(" "); 
     for (int x=0; x<splitted.length; x++){ 
      System.out.println(splitted[x]); 
     } 
    } 




    public void spellChecker(){ 
     Serializable data = new String[100]; 
     LinkedList<String> l=new LinkedList<String>(); 

     File f= new File("WordDict.txt"); 
     if(!f.exists()) 
      try { 
       f.createNewFile(); 
      } 
      catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
     try { 
      FileInputStream Fis=new FileInputStream(f); 
      Scanner sc = new Scanner(Fis); 
      System.out.println("Check outside nextline"); 

這是應該從.txt文件逐行讀取文字的地方,但它總是打破循環。

 while (sc.hasNextLine()){ 
        System.out.println("Check in nextline"); 
        data = sc.nextLine(); 
        l.add((String) data); 
       } 
       sc.close(); 
      } 
      catch(FileNotFoundException fnf){ 
       fnf.printStackTrace(); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
       System.out.println("\nProgram terminated Safely..."); 
      } 
      int x=0; 
      int y=0; 
      while(x<splitted.length){ 
       while(y<l.size()){ 
        if(l.get(y)==splitted[x]){ 
         System.out.println("Matched."); 
        y++; 
        }`enter code here` 
       } 
       System.out.println("Wrong spelling of: "+splitted[x]); 
       x++;  
      } 
     } 
    } 
+0

你試過使用讀取器而不是掃描儀? – luk2302

回答

2

明顯的原因似乎是,該文件WordDict.txt不存在, 所以你的代碼創建它,但它是空的,所以它沒有下一行。

在這段代碼,把一個斷點f.createNewFile()

File f= new File("WordDict.txt"); 
    if(!f.exists()) 
     try { 
      f.createNewFile(); 
     } 
     catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
    try { 
     FileInputStream Fis=new FileInputStream(f); 
     Scanner sc = new Scanner(Fis); 
     System.out.println("Check outside nextline"); 

另外一個明顯的原因可能是該文件存在,但它是空的。

很可能你的問題是第一個問題,你的困惑來自你對執行目錄的假設。也就是說,該程序可能不會在您想到的地方執行。要驗證什麼,請將WordDict.txt更改爲絕對路徑。

+0

謝謝janos!我的愚蠢是我拼錯了文件名。 – user5714576

+0

但仍然沒有按照我的要求工作。它從文件中讀取,但沒有比較..我修改了一下代碼。由於我是新手,我可以指導我如何向您展示新代碼? – user5714576

+0

這是一個新的代碼和一個新的問題,所以將其作爲一個新問題發佈。 – janos

0

sc.hasNextLine()只是因爲是空文件而返回true!試着寫上一些東西。

此外while循環是無限的,一個不正確康普艾(字符串== string是錯誤的):

 int x=0; 
     int y=0; 
     while(x<splitted.length){ 
      while(y<l.size()){ 
       if(l.get(y)==splitted[x]){ 
        System.out.println("Matched."); 
       y++; 
       }`enter code here` 
      } 
      System.out.println("Wrong spelling of: "+splitted[x]); 
      x++;  
     } 

這個循環也許應該是這樣的:

   int x=0; 
       int y=0; 
       while(x<splitted.length){ 
        while(y<l.size()){ 

         if(l.get(y).equals(splitted[x])){ 
          System.out.println("l.get("+ y +") is "+ l.get(y) +", splitted["+x+"] is "+ splitted[x]); 
          System.out.println("Matched."); 
         y++; 
         x++; 
         }else{ 
          System.out.println("Wrong spelling of: "+splitted[x]); 
          y++; 
         } 
        } 
       }