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 に答える