Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用wordclass查找给定单词的计数_Java_Object - Fatal编程技术网

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

Java 使用wordclass查找给定单词的计数,java,object,Java,Object,我有一个作业,其中我必须创建3个类:Oblig6(main方法)、Word(Ord)和Wordlist(Ordliste)。我必须使用word类找出文本中一个单词的重复次数 我在制定以下部分时遇到问题。我需要word类为给定的单词创建一个新的对象,如果它已经在wordlist(ArrayLis ordliste)中,那么下次它在文本中找到相同的单词时,它必须将Ord定义的特定对象的总数(字符串s)加1。我知道我正在创建一个新对象,每当它在单词列表中找到一个单词时,我需要一个关于如何正确表达它的建

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

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

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

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;
}
import java.util.Scanner;
导入java.io.File;
导入java.util.ArrayList;
公共类命令列表{
私有ArrayList ord=新ArrayList();
private ArrayList ordliste=new ArrayList();
私有整数i=0;
私有整数totalord=0;
私有int antallforekomster=0;
//读取提供的txt文件并将单词放入单词列表中
void lesBok(字符串filnavn)引发异常{
文件文件=新文件(filnavn);
Scanner innlestfil=新扫描仪(文件);
while(innlestfil.hasNextLine()){
add(innlestfil.nextLine());
}
}
//读取ord ArryList并将单词与ordliste arraylist进行比较,如果它们没有准备好,则添加它们
//如果有,则将该单词的新Ord(字符串s)对象装入板条箱,并添加到数量中。
void fraOrdtilOrdliste(){
ordliste.add(ord.get(i));

对于(i=0;i好的,让我试一试,因为我甚至不确定我是否正确阅读了您的代码

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

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

循环听起来真的不有效,但是如果你使用列表,那么这就是你真正能做的。所以你的性能基本上是O(nsquare),其中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);
            }
        }
    }
}

我不明白。你为什么不用一个静态方法将整个文本传递给word类,该方法返回要计算的word映射?我忘了提到我对编程非常陌生。如果你说的是为什么我不使用hashmap,那是因为我不被允许。是的,你实际上正确地阅读了代码,我99%肯定这个问题lem是循环每次在ordliste arraylist中找到与ord arraylist中的一个单词相同的单词时,都会生成一个新的对象单词,而不是查找该单词的旧对象并加1进行计数。我需要帮助制定此公式。不必担心效率问题,这来自java pr中的一个初学者类在我的大学编程时,我们还没有开始关注效率yeat。我为单词列表和单词类添加了一些示例代码。它应该可以工作,但我还没有测试过。顺便说一句,具体来说,您的代码的哪一部分是您在注释“每次循环生成一个新的对象单词…”中引用的循环如果您查看类Ordliste并找到方法fraOrdtilOrdliste,这是第二个方法,那么您将在第二个for循环中找到它。If(语句)new Ord(语句)。oekantallhmm…可能问题是您正在第一个语句Ordliste.add(Ord.get(i))中添加到列表中;在检查列表中是否已经存在单词之前,如何向ORD列表中添加单词。可能我误读了该代码。
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;
    }
}