0
我試圖讀取文件test.txt
,逐個獲取所有單詞,然後從讀取的單詞中刪除點和逗號。在字符串中刪除句號
這裏是我的代碼:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Utils {
public static void readFile(){
Scanner word = null;
try {
word = new Scanner(new File("test.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (word.hasNextLine()) {
Scanner s2 = new Scanner(word.nextLine());
Utils.cleanWord(s2);
while (s2.hasNext()) {
String s = s2.next();
System.out.println(s);
}
}
}
private static void cleanWord(String word){
word = word.replace(".", "");
}
當我編譯我的代碼,我得到這個錯誤Error:(18, 29) java: incompatible types: java.util.Scanner cannot be converted to java.lang.String
有誰知道我應該給這類型cleanWord
方法請,所以它可以執行刪除對單詞進行操作。 謝謝