hasNextLine()始终返回false

时间:2015-12-24 13:25:21

标签: java eclipse linked-list spell-checking

我试图制作一个拼写检查器并打开链接列表中的.txt文件中的单词,但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++;    
            }
        }
    }

2 个答案:

答案 0 :(得分: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更改为绝对路径。

答案 1 :(得分:0)

sc.hasNextLine()返回true只是因为是一个空文件!尝试在上面写点什么。

另外你的while循环是无限的,不正确的compair(string == 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++;
                        }
                    }
                }
相关问题