JAVA:我必须写一个程序来阅读"句子"从文件中,然后计算每个句子的单词和元音的数量

时间:2014-11-09 14:20:46

标签: java stringtokenizer

我正在开展一个项目,我想创建一个文件(要求用户输入'句子'),从中读取(读取这些句子),然后找到每个单词和元音的数量句子。 然后我打算以这种方式展示它们:

预期输出(忽略点,它们只是显示空格)

句子.........字数......元音数

句子1 ....... x ................................ y

当前输出 我得到一个输出,其中元音和单词的数量是前一个和当前元音和单词数的总和 例如:如果第一个句子有2个元音和2个单词,即使第二个句子的元音和单词数量相同,那么输出结果如下: 句子.........字数......元音数

句子1 ....... 2 ................................ 2

句子2 ....... 4 ................................ 4

所以我的问题是: 由于某种原因,我无法以我想要的方式打印(即获得每个句子的元音和单词的数量),你能帮我纠正我的程序吗?

这是我的代码:

import java.io.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
class asgn5_3
{
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static void main (String args[])throws IOException
    {
        System.out.println("Enter the number of sentences to be inputted");
        int sencounter=Integer.parseInt(br.readLine());
        String sentence="";

        int j = 0;//counter
        try//writing sentences to file
        {
            FileWriter fw=new FileWriter("c:\\users\\abc\\desktop\\TEXT.txt");
            BufferedWriter bw = new BufferedWriter(fw);            
            PrintWriter pw = new PrintWriter(bw);
            for (j=1;j<=sencounter;j++)
            {
                System.out.println("Enter your sentence:");
                sentence=br.readLine();
                pw.println(sentence);

            }    
            pw.close();
            bw.close();
            fw.close();
        }
        catch(IOException e)
        {
            System.out.println(e);
        }

        FileReader fr = new FileReader("c:\\users\\abc\\desktop\\TEXT.txt");//reading from a file
        BufferedReader sb = new BufferedReader (fr);
        String text;
        int i =0;//counter
        int numberofwords=0;
        int vowels=0;
        char ch;
        System.out.print("Sentence");
        System.out.print("          Number of words");
        System.out.println("          Number of vowels");

        while((text=sb.readLine())!=null)//counting words
        {
            StringTokenizer words = new StringTokenizer(text);
            numberofwords=numberofwords+words.countTokens();

            for (i=0;i<sentence.length();i++)//counting vowels
            {
                ch=sentence.charAt(i);
                if (ch=='a'||ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
                {
                    vowels++;
                }

            }
            System.out.print(sentence);
            System.out.print("          "+numberofwords);
            System.out.println("                        "+vowels);

        }    

    }
}    

1 个答案:

答案 0 :(得分:0)

几个问题:

  • 您正在使用上一个用户输入的句子,而您应该使用从下面的文件中读取的文字:

      StringTokenizer words = new StringTokenizer(text);
      numberofwords=numberofwords+words.countTokens();
    
      for (i=0;i<text.length();i++)
      {
          ch=text.charAt(i);
          if (ch=='a'||ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
          {
              vowels++;
          }
    
      }
      System.out.print(text);
    
  • 您继续重复计算,因此您会获得累计金额以避免在从文件中读取每一行时重置您的计数器,如下所示:

    元音= 0;
    numberofwords = 0;

所以你的代码应该是:

  while((text=sb.readLine())!=null)//counting words
  {
      vowels = 0;
      numberofwords = 0;
      StringTokenizer words = new StringTokenizer(text);
      numberofwords=numberofwords+words.countTokens();

      for (i=0;i<text.length();i++)
      {
          ch=text.charAt(i);
          if (ch=='a'||ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
          {
              vowels++;
          }

      }
      System.out.print(text);
      System.out.print("          "+numberofwords);
      System.out.println("                        "+vowels);

  }