0

So I am writing a program that reads a text file (in this case "votes.txt") and returns the number of strings and ranks them from most often to least often. So using votes.txt, the output should print the console as follows:

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

The errors I get are these:

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

Here is the code:

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 + ")";
        }
    }

}

Here is 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!

Any and all solutions are welcome. Thank you for reading!

  • 1
    Does this answer your question: https://stackoverflow.com/questions/10301907/why-do-i-get-non-static-variable-this-cannot-be-referenced-from-a-static-contex? – Brydenr Dec 12 '19 at 23:07

1 Answers1

0

If you follow the link in the comment, you will see that your Record class is an inner class of Profile, so you cannot access it from a static context since it's non-static. The best way to fix this is to move the class Record outside the class Profile.

Edit: Going off what David Conrad said in the comments, you could make that class static and keep it as an inner class, but I don’t advise it. I wasn’t going to mention it because I don’t advise doing it, but David is right, for sake of completeness it should be mentioned

  • 1
    Alternatively, OP could make `Record` a static class, but there's no reason for it to be an inner class at all. I include this comment only for the sake of completeness. :) – David Conrad Dec 13 '19 at 00:18