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
方法请,所以它可以执行删除对单词进行操作。 谢谢