计算一个文本文件中的唯一单词量? (不允许使用哈希) [英] count amount of unique words in a text file? (Not allowed to use Hash)

查看:103
本文介绍了计算一个文本文件中的唯一单词量? (不允许使用哈希)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能指望在一个文本文件中重复的单词,使用数组?

我的程序能够在文件中打印出总的话,但我怎么能得到我的程序打印不同的单词数量,还打印出这样的重复的单词数的列表:

蛋糕:4
一:320
海贼王:2
24

(大写字母和小写字母词被认为是同一个词)

 无效的FileReader(){
    的System.out.println(Oppgave A);
    INT totalWords = 0;
    INT uniqueWords = 0;
    的String []字=新的String [35000]
    的String [] = wordC新的String [3500];
    尝试{
        文件FR =新的文件(Alice.txt);
        扫描仪SC =新的扫描仪(FR);        而(sc.hasNext()){
        串词= sc.next();
        的String [] =空间words.split();
        的String [],逗号words.split();
            totalWords ++;
            }
        的System.out.println(Antall ORD SOM ER ER唯恐:+ totalWords);
    }赶上(例外五){        的System.out.println(找不到文件);    }


解决方案

每当你添加你需要检查的话阵列中已经存在的话。
为了比较,你将需要使用:

  word1.equalsIgnoreCase(字词);

How can I count repeated words in a text file, using an array?

My program is able to print out total words in the file, But how can I get my program to print the number of different words and also have printed out a list of the number of the repeated words like this:

Cake: 4 a: 320 Piece: 2 of 24

   (Words with capital letters and small letters are considered the same word)

void FileReader() { 


    System.out.println("Oppgave A");
    int totalWords = 0; 
    int uniqueWords = 0; 
    String [] word = new String[35000];
    String [] wordC = new String [3500];
    try {
        File fr = new File("Alice.txt");
        Scanner sc = new Scanner (fr);

        while(sc.hasNext()){
        String words = sc.next();
        String[] space = words.split(" ");
        String[] comma = words.split(",");
            totalWords++;


            }
        System.out.println("Antall ord som er lest er: " + totalWords);         
    } catch (Exception e) {

        System.out.println("File not found");

    }

解决方案

Every time you are adding a word you need to check if the word already exists in your array. To compare you will need to use:

 word1.equalsIgnoreCase(word2);

这篇关于计算一个文本文件中的唯一单词量? (不允许使用哈希)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆