I am trying to count words. It should count the same word once instead of twice. I can't seem to figure that out.
import java.util.*;
public class WordCount {
public static void main (String[]args)
{
final int LINES = 6;
Scanner in = new Scanner(System.in);
String paragraph = "";
System.out.println("Please input " + LINES + " lines of text. ");
for (int i = 0; i < LINES; i+=1)
{
paragraph = paragraph + " "+ in.nextLine();
}
System.out.println(paragraph);
int howlong = paragraph.length();
System.out.println(+howlong);
String word = " ";
int wordCount = 0;
for (int i = 0; i < paragraph.length(); i+=1)
{
if (paragraph.charAt(i) != ' ')
{
word ="";
if (paragraph.charAt(i+1) == ' ' || i+1 == paragraph.length()-1)
{
System.out.println(word);
wordCount ++;
word = "";
}
}
}
System.out.println("The number of words = " + wordCount);
}
}