2015-12-24 165 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++; 
         } 
        } 
       }