我試圖做一個拼寫檢查器,打開鏈接列表中的.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++;
}
}
}
你試過使用讀取器而不是掃描儀? – luk2302