0

我正在编写一个程序,它使用 Scanner 方法读取文本文件并输出:单词数、句子数、每句平均单词数、最长句子和最短句子。到目前为止,除了最长和最短的句子外,我什么都有,我似乎无法弄清楚。这是我到目前为止所拥有的...

import java.util.Scanner;
import java.io.*;
import java.io.IOException;
public class TestScanner {
  public static void main(String args[])
  {     Scanner in = new Scanner(System.in);
        String x = in.next();
        double count=0;
        int nbSentences = 0;
        while(in.hasNext())
        {   String word = in.next();
            nbSentences +=getNbSentences(word);
            count++;
        }
        System.out.println("Number of Words: "+ count);
        System.out.println("Number of Sentences: " + nbSentences);
        System.out.println("Average Words In Sentence: " + (count/nbSentences));
        System.out.println("Longest Sentence: ");
        System.out.println("Shortest Sentence: ");
 }
 //**************************number of sentences*********************************
 public static int getNbSentences(String word) 
 {         int result = 0;
       char[] chars = word.toCharArray();
       for(Character c : chars) 
       {    if(c == '.' || c == '!' || c == '?') 
            {   result++; 
            }
       }
       return result;
 }
 //*************************Longest Sentence*************************************


 //This is where I'm stuck....

}

如果有人可以提供帮助,我将不胜感激!!

4

4 回答 4

1

下面的代码从文本文件中找到最长和最短的行以及行号。(限制:如果两行相同,则将第一行视为长行或短行)。我使用 HashMap 来存储行(行号作为键,行内容作为值)

如果需要在此代码中添加任何内容,请发布。

` 封装测试;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class LongestAndShortestLineFinder {

static int previousLongLine = 0;
static int previousShortLine = 10000;

public void printLongLine(HashMap longLineMap) {

    Set keyofSet = longLineMap.keySet();
    Iterator itr = keyofSet.iterator();
    while (itr.hasNext()) {
        Integer keys = (Integer) itr.next();
        String value = (String) longLineMap.get(keys);
        System.out.println("Line Number of Longest line: " + keys
                + "\nLongest line: " + value);
    }
}

public void printShortLine(HashMap shortLineMap) {

    Set keyofSet = shortLineMap.keySet();
    Iterator itr = keyofSet.iterator();
    while (itr.hasNext()) {
        Integer keys = (Integer) itr.next();
        String value = (String) shortLineMap.get(keys);

        System.out.println("Line Number of Shortest line: " + keys
                + "\nShortest line: " + value);
    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String fileName = "d:\\test.txt";
    // This will reference one line at a time
    String line = null;
    int key = 0;
    int lineSize = 0, lineNumber = 0;

    LongestAndShortestLineFinder ln = new LongestAndShortestLineFinder();

    HashMap longLineMap = new HashMap();
    HashMap shortLineMap = new HashMap();

    try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while ((line = bufferedReader.readLine()) != null) {
            lineNumber++;
            lineSize = line.length();
            if (lineSize > previousLongLine) {
                previousLongLine = lineSize;
                longLineMap.clear();
                longLineMap.put(lineNumber, line);
            }
            if (lineSize < previousShortLine) {
                previousShortLine = lineSize;
                shortLineMap.clear();
                shortLineMap.put(lineNumber, line);
            }

        }
        // Always close files.
        bufferedReader.close();
    } catch (FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");
    } catch (IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");
        // Or we could just do this:
        // ex.printStackTrace();
    }

    ln.printLongLine(longLineMap);
    ln.printShortLine(shortLineMap);
}

 }

`

于 2014-06-22T17:52:25.017 回答
1
  1. 初始化int currentSentenceLength = 0int maxSentenceLength = 0在循环之外。
  2. 对于每个word:增量currentSentenceLength
  3. 如果word包含.!或者?做:maxSentenceLength = Math.max(currentSentenceLength, maxSentenceLength); currentSentenceLength = 0;
  4. 返回maxSentenceLength

你可以对最短的句子做同样的事情。

于 2013-03-06T09:43:13.643 回答
1

伪代码:

  1. 将第一句设置为最长和最短。
  2. 遍历所有句子,将它们与最短和最长的进行比较。
  3. 如果您发现比 shortest 更短的句子,请将其用作 shortest。
  4. 如果您发现一个比最长的句子更长的句子,请将其用作最长的。
于 2013-03-06T09:41:28.630 回答
0

根据您的源代码,您需要做的是获取句子中的单词数。然后,您可以更新将在句子存储中存储当前最小/最大单词数的变量,或者将句子中的单词数作为一个集合并获得最大、最小(最终是平均值)后验。

main显式:将方法中的相应行更改为

double count = 0;
int nbSentences = 0;
int nbWordsInASentence = 0;
Collection nbWordsInSentences = new ArrayList<Integer>();
while (in.hasNext())
{   String word = in.next();
    count++;
    nbWordsInASentence++;

    int incrementNbSentences = getNbSentences(word);
    if (incrementNbSentences > 0) {
        nbWordsInASentence.add(nbWordsInASentence);
        nbWordsInASentence = 0;
    }
}

int maxSentenceLength;
int minSentenceLength;
if (nbWordsInSentences.size() > 0)
{   maxSentenceLength = Collections.max(nbWordsInSentences);
    minSentenceLength = Collections.min(nbWordsInSentences);
}
else
{   maxSentenceLength = 0; // use whatever value seems appropriate
    minSentenceLength = 0; // use whatever value seems appropriate
}

但这仅在结果getNbSentences从不大于 1 时才有效(输入参数是一个单词这一事实暗示了这一点)。

请注意,该方法getNbSentences的实现在悬挂标记的情况下可能不正确:该方法在这种情况下返回 3。

于 2013-03-06T09:41:54.697 回答