我正在一个项目中,我想创建一个文件(要求用户输入“句子”),从中读取文件(读取这些句子),然后查找每个句子中的单词和元音数量。然后我计划以这种方式显示它们:
预期的输出:(忽略点,它们只是用来显示空格)
句子.........字数......元音数
句子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);
}
}
}
几个问题:
您正在使用最后一个用户输入的句子,而您应该使用从文件中读取的文本,如下所示:
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;
字数= 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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句