有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用wordclass查找给定单词的计数

我有一个作业,我必须创建3个类:Oblig6(main method)、Word(Ord)和Wordlist(Ordliste)。我必须找到一个单词在文本中使用word类重复的次数

我在制定以下部分时遇到问题。我需要word类为给定的单词创建一个新的对象,如果它已经在wordlist(ArrayLis ordliste)中,那么下次它在文本中找到同一个单词时,它必须将Ord定义的特定对象的总数(字符串s)加1。我知道我正在创建一个新对象,每当它在单词列表中找到一个单词时,我需要一个关于如何正确表达它的建议。 这是我的密码

在wordlist类中,主要问题是在void fraOrdtilOrdliste中

import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;

public class Ordliste {
private ArrayList<String> ord = new ArrayList<String>();
private ArrayList<String> ordliste = new ArrayList<String>();
private int i = 0;
private int totalord = 0;
private int antallforekomster = 0;


// Reads the provided txt file and puts the words into a word list

void lesBok(String filnavn) throws Exception {
    File file = new File(filnavn);
    Scanner innlestfil = new Scanner(file);
    while (innlestfil.hasNextLine()) {
        ord.add(innlestfil.nextLine());
    }
}

// Reads ord arryalist and compares the words to ordliste arraylist, adds them if they are not  inn it all ready
//If they are there, crates a new Ord(String s)object of that words and adds to amount.
void fraOrdtilOrdliste () {
    ordliste.add(ord.get(i));
    for (i=0;i<ord.size();i++) {
        Boolean unik = true;
        for (int j = 0; j<ordliste.size();j++) {
            if (ordliste.get(j).equalsIgnoreCase(ord.get(i))) {
                unik = false;
                new Ord(ordliste.get(j)).oekAntall();
            }
        }
        if (unik) {
            ordliste.add(ord.get(i));
        }
    }

}

// Using the Ord class as a counter for this method. If the word is registerd beforhand it will add 1.

void leggTilOrd(String s) {
    for (i = 0; i < ord.size(); i++) {
        if (ord.get(i).equalsIgnoreCase(s)) {
            ord.add(i, s);
            System.out.println("Suksess");
        } else if (!ord.get(i).equalsIgnoreCase(s)) {
            new Ord(s).oekAntall();
            System.out.println("Antall okt");
            return;
        }
    }
}

// Searches for the word in the wordlist and returns null if it does not exist.
Ord finnOrd(String s) {
    for (i = 0; i < ord.size(); i++) {
        if (!s.equalsIgnoreCase(ord.get(i))) {
            System.out.println("null");
            return null;
        } else if (s.equalsIgnoreCase(ord.get(i))) {
            System.out.println("Fant ordet");
        }

    }
    return null;
}

// Prints out the total amount of words in the word list.
int antallOrd() {
    for (i = 0; i < ordliste.size(); i++) {
        totalord++;
    }
    System.out.println("Antall ord i ordlisten er: " + totalord);
    return totalord;
}

// Counts the total amounts of a word in the word list.
int antallForekomster(String s){
   antallforekomster= new Ord(s).hentAntall();
   System.out.println("Ordet forekommer " + antallforekomster + " ganger");
   return antallforekomster;
}

她的是单词class


共 (1) 个答案

  1. # 1 楼答案

    好吧,让我试一试,因为我甚至不确定我是否正确阅读了你的代码

    a)定义一个类字,该类字的计数有一个成员变量,字符串字有一个成员变量

    b)在wordlist类中有一个成员变量,它是一个列表。每次你解析一个单词时,在列表中循环,比较你的字符串和单词的字符串。如果匹配,则增加word类中的计数

    这个循环听起来真的很无效,但如果你使用一个列表,那么这就是你真正能做的。所以你的表现基本上是O(n平方),其中n是给定文本中的字数

    单词列表类:

    public class WordList {
        static List<Word> words = new ArrayList<Word>();
        public static void countWord(String inputWord) {
            for (Word word : words) {
                if (word.getWord().equals(inputWord)) {
                    word.setCount(word.getCount() + 1);
                } else {
                    Word newWord = new Word();
                    newWord.setWord(inputWord);
                    newWord.setCount(1);
                    words.add(newWord);
                }
            }
        }
    }
    

    词类:

    public class Word {
        String word;
        int count;
    
        public String getWord() {
            return word;
        }
    
        public void setWord(String word) {
            this.word = word;
        }
    
        public int getCount() {
            return count;
        }
    
        public void setCount(int count) {
            this.count = count;
        }
    }