hasNextLine() always returns false

user5714576

I'm trying to make a spell checker and going to open the words in .txt file in a linked list but hasNextLine() always returns 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");

this is the point where it should take words line by line from .txt file but it always breaks the loop.

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

The obvious reason seems to be that the file WordDict.txt doesn't exist, so your code creates it, but it's empty, so it has no next line.

In this code, put a breakpoint on 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");

Another obvious reason can be that the file exists but it's empty.

Most probably your problem is the first one, and your confusion comes from your assumption of the execution directory. That is, the program is probably not executed where you think. To verify what, change WordDict.txt to absolute path.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related