列表排序程序的代码中的静态错误

时间:2015-11-04 05:32:54

标签: java static compiler-errors sortedlist

我正在创建一个程序,它从文本文件中获取单词并将所有单词按字典顺序排列,然后计算频率。它们分为两列,如下所示:

Word      Occurs
====      ======
a           21
animal       3 
. 
. 
.
zebra        1
====      ======
Total     1325

我尝试编译所有代码并收到此错误:

Count2.java:12: error: non-static variable this cannot be referenced from a 
static context
    List sortedList = new List();

这是我的代码有什么问题导致它得到这个静态错误?

import java.util.Scanner;

public class Count2
{
    static Scanner stdin = new Scanner(System.in);

    public static void main(String[] args)
    {
    String k = nextWord();
    List sortedList = new List();
    while(k != null)
    {
        sortedList.insert(k);
        k = nextWord();
    }

    sortedList.print();

}

    private static String nextWord()
    {

    if(stdin.hasNext())
    {
        String word = stdin.next();
        word = word.toLowerCase();
        int start = 0;
        int end = word.length();

        for(int c = 0; c < word.length(); ++c)
        {
            if(Character.isLetter(word.charAt(c)) || word.charAt(c) == '-') 
            {
                start = c;
                break;
            }

        for(int n = start; n < word.length(); ++n)
        {
            if(!(Character.isLetter(word.charAt(n)) || word.charAt(n) == '-'))
            {
                end = n;
                break;
            }
        }
        return word.substring(start,end);
    }

    return null;


    } // nextWord


} // end Count2

class List
{
    public class Node
    {
        String word;
        Node next;
        int count;

        public Node(String Words, Node Next)
        {  
            word = Words; 
            next = Next; 
            count = 1; // number of word occurences
        }
    }
    private Node first;
    private int numWords;

    public List() //make an empty list
    { 
        first = null; 
        numWords = 0; 
    }

    public void insert(String word)
    {    
        if(first == null) 
        {
            Node newNode;
            newNode = addNode(word, null);
            first = newNode;          
        }   
        else if(word.equals(first.word)) //first != null, check if word matches first word on List
        {
            first.count++;
        }
        else // first != null and first != first word
        {  
            Node newNode;
            Node current;
            current = first;
            Node previous;
            previous = null;

            int c =  word.compareTo(current.word);

            while ((c > 0) && (current.next != null)) //loop when c is positive
            {
                previous = current;    
                current = current.next;
                c =  word.compareTo(current.word);
            }

            if ((c >0 && current.next == null)) 
            {
                newNode = addNode(word, null);
                current.next = newNode;
            }
            else if (c==0) // increase count when word exists 
            {
                current.count++;
            }
            else 
            { 
                newNode = addNode(word, current);

                if (previous == null) //comes after new word 
                {
                    first = newNode;       
                }         

                else //insert node in middle of list
                {
                    previous.next = newNode;
                }
            }       
        }
    }

    private Node addNode(String word, Node next) //adds new Node and increase counter
    {
        Node newNode = new Node(word, next);
        numWords++;
        return newNode;
    }

    public String[] wordList() 
    {
        String[] WORDS = new String[numWords];
        Node current = first;
        int i =0;

        while (current != null) {     
            WORDS[i] = current.word;
            current = current.next;
            i++;
        }
        return WORDS;
    }   

    public int[] getFrequency() //int array for amount of times a word occurs
    {
        int[] numbers = new int[numWords];
        Node current = first;
        int i =0;

        while (current != null) {
            numbers[i] = current.count;
            current = current.next;
            i++;
        }
        return numbers;
    }

    public void print() // prints words in the list and number of times each word occurs
    {
        int[] numbers = getFrequency();
        String[] WORDS = wordList();

        System.out.println("Word   \t    \t    Occurs");
        System.out.println("====   \t    \t    ======");

        for (int i =0; i < numWords; i++) 
        { 
            System.out.println(WORDS[i] + " \t " + numbers[i] );   
        }
    }      

}
}

1 个答案:

答案 0 :(得分:0)

两个问题

1。)方法nextWord()应该有一个返回类型。

2。)List sortedList = new List();不是处理内部类的有效对象。

更改为,

List sortedList = new Count().new List();