Reputation: 1
Hi I'm trying to display the number of all three and five letter words from a text file called Article.txt but the output I get is 4 for both. I am a beginner and I will appreciate any kind of help. Thank you!
import java.util.*;
import java.io.*;
class test
{
public static void main(String[] args) throws Exception
{
FileReader fr = new FileReader("E:\\test\\Article.txt");
Scanner s = new Scanner(fr);
String str = s.nextLine();
String[] words = str.split(" ");
int countThree = 0, countFive = 0;`
for(String word : words)
{
if(word.length() == 3)
{
countThree++;
}
else if(word.length() == 5)
{
countFive++;
}
}
System.out.println("Number of three letter words: " +countThree);
System.out.println("Number of five lettr words: " +countFive);
}
}
Here is the article:
There was a time when Pete Sampras tally of 14 Grand Slam singles titles the last of which came at the US Open in 2002 seemed like the acme of sporting achievement in men tennis. Little did anybody expect that in the next 16 years across 64 Majors not one or two but three players would stand shoulder to shoulder with the American great. On Sunday Novak Djokovic became that third man defeating Argentine Juan Martin del Potro for his third US Open title at Flushing Meadows. The 31 year old Serb has never been considered a once in a generation talent as have Roger Federer and Rafael Nadal the ones above him in the trophy count. But nobody represents the modern day game as well as Djokovic. He is the ultimate practitioner of the attrition-based baseline tennis and at his best with his supremely efficient patrolling of the court is near invincible. Over two weeks in New York he hit this high many times over. In fact the 95-minute second set in the final was a microcosm of Djokovic last two years. It was long and weary as fortunes swung back and forth. But adversity energised him and he found a level which his opponent could not match. Coming after his triumphant return at Wimbledon in July the latest success is evidence enough that technically, tactically and physically Djokovic is back to his best. If it was about the restoration of the old order on the mens side it was the continuation of the new in the women section. There has been a first time winner in four of the past six Grand Slam tournaments and 20 year old Naomi Osaka added to the eclectic mix by becoming the first Japanese to win a Major. In Serena Williams the winner of 23 singles Slams, the most by any player in the Open Era Osaka faced the ultimate challenge. It was also an inter generational battle like none other. The 16 year age gap between Williams and Osaka was the second biggest in the Open Era for a womens final next only to Monica Seles versus Martina Navratilova at the 1991 US Open. To her immense credit Osaka was not awed by the stage. While growing up she had revered Williams. After all this is someone who chose Williams as her subject for a school essay in third grade. On Saturday she played like she knew the 36 year olds game like the back of her hand absorbing everything the American threw at her and redirecting them with much more panache. The magnitude of her achievement was nearly drowned out by the chaos in the aftermath of Williams tirade against the chair umpire. Yet the manner in which Osaka at an impressionable young age closed out the match with a cold relentlessness showed she is here to stay.
Upvotes: 0
Views: 30
Reputation: 3608
I assume you would like to process your file by line. At present you are only evaluating the first line by executing
String str = s.nextLine();
For this line you are counting the number of words.
You have to count also for all other lines.
Upvotes: 1