2015-10-18 44 views
0

这就是我的 没有错误显示,但它不运行请告诉我什么问题 我是否必须导入其他? 该文件是书中的一段用逗号分隔文件中的单词Java?

import java.io.File; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Unique { 

    public static void main(String[] args) { 

    } 

    public void add(String fileName) throws Exception {  
     File inFile = new File("ReadThis.txt"); 
     ArrayList<String> words = new ArrayList<String>(); 
     Scanner scanner = new Scanner(inFile); 
     while (scanner.hasNext()) { 
      String word = scanner.next() ;      
      word = word.replaceAll("[^a-zA-Z ]", "");   
      words.add(word) ;         
     } 
     scanner.close(); 
    } 
} 
+1

您的主要方法不包含任何指令。所以没有任何反应即使它调用了add()方法,该方法也会读取文件并将字符串存储在列表中,但它不会对该列表执行任何操作。这只是一个昂贵的诺言。 –

回答

1

代码的入口点是空的。

public static void main(String[] args) { 

} 

您所描述的行为正是这个代码:什么

您必须将要运行的代码插入main-method才能使其运行。例如:

public static void main(String[] args) { 
    new Unique().add("someFile"); 
}