2014-02-07 44 views
0

我无法更改程序的shell,最终目标是从txt文件中的单词列表中选择一个随机单词。我已经扫描了很多遍,遍历代码,尝试了很多不同的东西,但每次运行它时,它都会编译而不会出现问题,但我从来没有得到任何输出。我甚至尝试在私有函数中插入一些输出,但无济于事。任何人都可以看到我的代码有什么问题,或者可以向我解释发生了什么?从txt文件中检索随机单词并且没有输出,并且没有编译器错误java

import java.util.*; 

    class PartOfSpeech 
    { 
     private String[] words; 
     private Random random; 
     private String filename; 

     public PartOfSpeech(String filename) 
     { 
     this.filename = filename; 
     this.read(); 
     } 
     //this picks a random number and uses that number for the index of the array for which to return 
     public String getRandomWord() 
     { 
     int index; 
     index = random.nextInt(this.getCount()); 
     return words[index]; 
     } 
     //this gets a count of how many lines of txt are in the file 
     private int getCount() 
     { 
     Scanner fr = new Scanner(this.filename); 
     int count = 0; 
     while(fr.hasNextLine()) 
     { 
     count++; 
     } 
     return count; 
     } 
     //this creates a scanner and inserts each word from the txt file into an array 
     private void read() 
     { 
     Scanner fr = new Scanner(this.filename); 
     for(int i=0; i<this.getCount(); i++) 
     { 
     words[i] = fr.nextLine(); 
     } 
     } 

     public static void main(String[] args) 
     { 
     PartOfSpeech n = new PartOfSpeech("nouns.txt"); 
     System.out.print(n.getRandomWord()); 
     } 
    } 
+0

你曾经调用函数'read'吗? –

+0

this.read()在构造函数 – amudhan3093

+0

啊是的,我看到'getCount'返回什么? –

回答

1

构造扫描仪(字符串源)实际分析源字符串的内容,而不是把它作为一个文件名,你需要

new Scanner(new File(fileName)) 
1

根据Oracle文档,你应该使用new File作为Scanner的参数。

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

private void read() 
    { 
    Scanner fr = new Scanner(new File(this.filename)); 
    for(int i=0; i<this.getCount(); i++) 
    { 
    words[i] = fr.nextLine(); 
    } 
    } 

无关的问题,但你应该认真考虑重写这个功能:

//this gets a count of how many lines of txt are in the file 
    private int getCount() 
    { 
    Scanner fr = new Scanner(this.filename); 
    int count = 0; 
    while(fr.hasNextLine()) 
    { 
    count++; 
    } 
    return count; 
    } 

当你读文件一次把所有的话,你应该更新计数值,而不是在getCount中多次重新打开文件。如果文件更改,count将与words中的项目数量不同。

我重构代码以这样的事有一个ArrayList,而不是[]:

private void read() 
    { 
    Scanner fr = new Scanner(new File(this.filename)); 

    // reloading the file should clear the collection first 
    words.clear() 

    while(fr.hasNextLine()) 
    { 
    words.add(fr.nextLine()); 
    } 
    } 

    private int getCount() 
    { 
    return words.size(); 
    } 

你或许能够摆脱完全getCount,如果它不是任何地方使用,并且只使用words.length。当多次拨打read函数时,如果可以在两者之间添加单词,则应清除收集。否则,您可以跳过所有元素,直到您已经在的行,然后向集合添加更多元素。

1

我会建议重新考虑你的结构。您不知道文件中会有多少字,因此您应该使用Collection<String>而不是一些固定的String[],以避免重复多次。也许你可以尝试这样的:

import java.io.File; 
import java.util.Collections; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Scanner; 

public class PartsOfSpeech { 

    private final List<String> words; 
    private final File file; 

    private int index; 

    public PartsOfSpeech(final String filePath){ 
     words = new LinkedList<>(); 

     file = new File(filePath); 
     read(); 

     Collections.shuffle(words); 
    } 

    private void read(){ 
     try{ 
      final Scanner input = new Scanner(file, "UTF-8"); 
      while(input.hasNextLine()) 
       words.add(input.nextLine()); 
      input.close(); 
     }catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 

    public String getRandomWord(){ 
     if(index == words.size()){ 
      index = 0; 
      Collections.shuffle(words); 
     } 
     return words.isEmpty() ? null : words.get(index++); 
    } 

    public static void main(String[] args){ 
     final PartsOfSpeech pos = new PartsOfSpeech("noun.txt"); 
     System.out.println(pos.getRandomWord()); 
    } 
} 
0
  1. 你的实例变量随机未初始化,您将获得NPE。
  2. 按照其他人的建议使用新文件(this.filename)。
  3. 由于您没有调用Scanner.next(),因此您的getCount方法停留在无限循环中。
  4. 按照其他人的建议使用集合对象。
  5. 每次需要计数时,都不需要遍历整个列表。
  6. 这是一个很好的做法,尽量减少使用或完全避免使用实例变量。
0

我建议你只读一次你的文件到一个字符串列表中。那么你的计数方法就是在你的列表中调用size()。这里有一种方法可以用来读取你的文件并解析成字符串列表:

public List<String> readFile(String filePath) throws IOException { 
    List<String> result = new ArrayList<>(); 
    try (BufferedReader reader = new BufferedReader(
      new InputStreamReader(
        new FileInputStream(filePath)))) { 
     String line; 
     while ((line = reader.readLine()) != null) { 
      result.add(line.replace("\n", "")); 
     } 
    } 

    return result; 
} 
相关问题