我正在尝试创建一个程序,它从文件中读取文本 alice.txt 并找到 txt 文件中的单词并计算单词的数量。我不断收到一些错误,我不知道为什么
这是我的代码:
import easyIO.*;
import java.util.Scanner.*;
class Oblig3A {
//
// Main-metoden
//
public static void main (String[]args){
WordAnalysis a = new WordAnalysis();
a.ReadFile();
}
}
//
// WordAnalysis-class
//
class WordAnalysis{
String[] ord = new String [5000];
int[] antall = new int [5000];
int antUnikeOrd = 0;
void ReadFile(){
Scanner read = new Scanner(new File("Alice.txt"));
String[] ord = new String[5000];
int[] antall = new int[5000];
while(read.hasNext()) {
// obtains words from line
String[] word = read.next();
String[] arrWord = word.split(" ");
// Loops all words from line
for (int i = 0; i < arrWord.length; i++) {
// Checks if the word is in the list or not
if (!ord.contains(arrWord[i])) {
// Word is not in the list from before, add and set amount to 1
ord.append(arrWord[i]);
antall[ord.length-1] = 1;
}
else {
// Words exists from before, find indez in array word and increase amount +1
int preAntall = Arrays.asList(ord).indexOf(arrWord[i]);
antall[preAntall] += 1;
}
}
}
// Debug
System.out.println("Number of unique words: "+ord.length);
// prints out
for (int i = 0; i < ord.length; i++) {
System.out.println(ord[i] + " exists " + antall[i] + " times.");
}
}
}
错误:
Oblig3A.java:41: error: cannot find symbol
Scanner read = new Scanner(new File("Alice.txt"));
^
symbol: class Scanner
location: class WordAnalysis
Oblig3A.java:41: error: cannot find symbol
Scanner read = new Scanner(new File("Alice.txt"));
^
symbol: class Scanner
location: class WordAnalysis
Oblig3A.java:41: error: cannot find symbol
Scanner read = new Scanner(new File("Alice.txt"));
^
symbol: class File
location: class WordAnalysis
Oblig3A.java:50: error: cannot find symbol
String[] arrWord = word.split(" ");
^
symbol: method split(String)
location: variable word of type String[]
Oblig3A.java:57: error: cannot find symbol
if (!ord.contains(arrWord[i])) {
^
symbol: method contains(String)
location: variable ord of type String[]
Oblig3A.java:61: error: cannot find symbol
ord.append(arrWord[i]);
^
symbol: method append(String)
location: variable ord of type String[]
Oblig3A.java:67: error: cannot find symbol
int preAntall = Arrays.asList(ord).index
Of(arrWord[i]);
^
symbol: variable Arrays
location: class WordAnalysis
7 errors
如果有人能告诉我如何解决这个问题,我将不胜感激。
非常感谢!