【问题标题】:hasNextLine() always returns falsehasNextLine() 总是返回 false
【发布时间】:2016-03-30 22:36:00
【问题描述】:

我正在尝试做一个拼写检查器并打算在链接列表中打开 .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++;    
            }
        }
    }

【问题讨论】:

  • 你试过用阅读器代替扫描仪吗?

标签: java eclipse linked-list spell-checking


【解决方案1】:

sc.hasNextLine() 返回 true 只是因为它是一个空文件!!!试着在上面写点东西。

此外,您的 while 循环是无限的,并且比较不正确(字符串 == 字符串是错误的):

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

【讨论】:

    【解决方案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 更改为绝对路径。

    【讨论】:

    • 谢谢亚诺斯!文件名拼错是我的愚蠢。
    • 但是,它仍然没有按我的意愿工作。它从文件中读取但不比较..我对代码进行了一些修改..由于我是 stackoverflow 的新手,您能指导我如何向您展示新代码吗?
    • 这是一个新代码,一个新问题,所以将它作为一个新问题发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2021-11-05
    • 2018-11-05
    • 2019-05-06
    相关资源
    最近更新 更多