0

我正在尝试创建一个程序,它从文件中读取文本 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

如果有人能告诉我如何解决这个问题,我将不胜感激。

非常感谢!

4

2 回答 2

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];
于 2013-10-03T17:07:00.600 回答
1

See if this is what you expect, changed your program itself a bit

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.");       }   } }
于 2013-10-03T17:49:02.110 回答