I am working on a project where I wanted to create a file(ask user to input 'sentences'), read from it(read those sentences) and then find the number of words and vowels in EACH sentence. Then I had planned to display them is this manner:
EXPECTED OUTPUT:(ignore the dots, they are just there to show spaces)
Sentence......... Number of Words...... Number of vowels
sentence 1.......x................................y
CURRENT OUTPUT I am getting an output where the number of vowels and words is the sum of the previous and current number of vowels and words example: if the 1st sentence had 2 vowels and 2 words and even the second sentence would have the same number of vowels and words then the output would be: Sentence......... Number of Words...... Number of vowels
sentence 1.......2................................2
sentence 2.......4................................4
So my question is that: For some reason i am not able to print in the way I want to (i.e-get number of vowels and words for EACH sentence), could you please help me rectify my program?
here is my code:
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);
}
}
}