2015-11-04 79 views
1

我正在创建一个程序,它从文本文件中提取单词,并将所有单词按字典顺序排列,然后再对频率进行计数。这些投入两列,看起来像这样:列表排序程序的代码中的静态错误

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

您应该考虑'List'到'MyList'或别的东西改变内部类的名称,因为它看起来像'java.util.List'接口。 –

+0

我试过了,它不起作用。它不会识别它,即使我将它更改为“我的列表” – anderkat97

回答

0

两个问题

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

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

变化,

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

downvote为什么,我在eclipse中运行这个,这是我得到的错误 –

+0

我upvoted你。这个答案对我来说很合适。 –