我正在尝试创建一个程序,它从文件中读取文本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
如果有人能告诉我如何解决这个问题,我将不胜感激。
非常感谢!
答案 0 :(得分:3)
编译错误告诉您错误是什么。
Java要求在可以使用非限定格式之前先导入类
import java.util.Arrays;
import java.io.File;
本声明
import java.util.Scanner.*;
使用无效语法导入单个类。你需要
import java.util.Scanner;
Arrays
没有contains
方法(或任何方法),因此您需要
List<String> list = new ArrayList<>();
而不是
String[] ord = new String[5000];
答案 1 :(得分:1)
看看这是否符合预期,稍微改变了程序本身
import java.awt.List; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner;
class Oblig3A {
// // Main-metoden //
public static void main (String[]args) throws FileNotFoundException{
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() throws FileNotFoundException{ Scanner read = new Scanner(new File("Alice.txt")); String ord = new String(); int[] antall = new int[5000];
String word=null;
while(read.hasNext()) {
// obtains words from line
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].toString())) {
// Word is not in the list from before, add and set amount to 1
ord += 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 + " exists " + antall[i] + " times."); } } }