【问题标题】:Static error in code for list sorting program列表排序程序代码中的静态错误
【发布时间】:2015-11-04 05:32:54
【问题描述】:

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

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] );   
        }
    }      

}
}

【问题讨论】:

  • 您应该考虑将内部类的名称从List 更改为MyList 或其他名称,因为它看起来像java.util.List 接口。
  • 我试过了,但它不起作用。即使我将其更改为MyList,它也无法识别它

标签: java static compiler-errors sortedlist


【解决方案1】:

两个问题

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

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

改成,

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

【讨论】:

  • 投反对票,我在 Eclipse 中运行了这个,这是我得到的错误
猜你喜欢
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多