【问题标题】:I am trying to parse a text file to counter the number of unique strings and print some information about those strings我正在尝试解析文本文件以计算唯一字符串的数量并打印有关这些字符串的一些信息
【发布时间】:2019-12-12 22:58:23
【问题描述】:

所以我正在编写一个程序,它读取一个文本文件(在本例中为“votes.txt”)并返回字符串的数量并将它们从最频繁到最不频繁排列。所以使用 votes.txt,输出应该打印控制台如下:

1. Trump = 7
2. Hillary = 7
3. Bernie = 6
4. Jeb! = 5
5. Putin = 3
6. Colbert = 1
7. Stein = 1

我得到的错误是:

Error:(26, 29) java: non-static variable this cannot be referenced from a static context
Error:(21, 28) java: non-static variable this cannot be referenced from a static context

代码如下:

import java.io.FileNotFoundException;
import java.io.File;
import java.util.*;

public class Profile {

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("votes.txt");
        Scanner inFile = new Scanner(file);
        HashMap<String, Integer> map = new HashMap<>();
        String input = inFile.next();

        while(inFile.hasNext());{
            if(!map.containsKey(input)){
                map.put(input, 1);
            }
            else{
                map.put(input, map.get(input) + 1);
            }
        }
        Record[] records = new Record(map.size());
        Iterator<String> iterator = map.keySet().iterator();
        int index = 0;
        while(iterator.hasNext()){
            String key = iterator.next();
            Record record = new Record(key, map.get(key));
            records[index] = record;
            index++;
        }
        Arrays.sort(records, Collections.reverseOrder());
        for(int i = 0; i < 5; i++){
            System.out.println(records[i]);
        }
    }
    class Record implements Comparable<Record>{
        String key;
        int count;
        public Record(String key, int count){
            this.key = key;
            this.count = count;
        }
        public int compareTo(Record other){
            if(this.count < other.count){
                return -1;
            }
            else if (this.count > other.count){
                return 1;
            }
            else{
                return  0;
            }
        }
        public String toString(){
            return  "(" + key + ", " + count + ")";
        }
    }

}

这是 votes.txt:

Bernie Trump Bernie Bernie
  Trump Trump Hillary

                Jeb!

Hillary Trump Bernie Hillary
   Bernie
Putin  Putin Putin
 Hillary
   Bernie
  Hillary Hillary Hillary Trump


  Colbert

               Jeb!     Jeb!
    Trump

          Johnson
                        Stein
 Jeb!  Jeb!

欢迎任何和所有解决方案。感谢您的阅读!

【问题讨论】:

标签: java file maps voting


【解决方案1】:

如果您点击评论中的链接,您将看到您的Record classProfile 的内部类,因此您无法从static 上下文访问它,因为它是non-static。解决此问题的最佳方法是将class Record 移到class Profile 之外。

编辑: 偏离大卫康拉德在 cmets 中所说的内容,您可以将该类设为静态并将其保留为内部类,但我不建议这样做。我不打算提及它,因为我不建议这样做,但大卫是对的,为了完整起见,应该提及它

【讨论】:

  • 另外,OP 可以使Record 成为一个静态类,但根本没有理由让它成为一个内部类。为了完整起见,我仅包含此评论。 :)
猜你喜欢
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
相关资源
最近更新 更多