我試圖獲得「Lenore」在詩中的次數以及總字數。我收到第13行的錯誤,請幫忙。我很新,似乎無法掌握如何正確地訂購代碼。標識符錯誤?
package theraven;
import java.io.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class lenore {
Scanner myscanner = new Scanner("/Users/karaleamann/Desktop/theraven.txt");
public int countWord(String Lenore, File"/Users/karaleamann/Desktop/theraven.txt") {
int count = 0;
while (myscanner.hasNextLine()) {
String nextToken = myscanner.next();
if (nextToken.equalsIgnoreCase(Lenore))
count++;
}
return count;
}
public int countAll() {
File file = new File("/Users/karaleamann/Desktop/theraven.txt");
Scanner sc = null;
try {
sc = new Scanner(new FileInputStream(file));
} catch (FileNotFoundException ex) {
Logger.getLogger(lenore.class.getName()).log(Level.SEVERE, null, ex);
}
int count = 0;
while (sc.hasNext()) {
sc.next();
count++;
}
System.out.println("Number of words: " + count);
return 0;
}
}
調用Lenore的方法似乎也有問題。在主要我有:lenore myLenore = new lenore(); myLenore.countWord();但我不確定要在()中放什麼? – user3085151