hey guys , Im also working on this word coiunt program and it seems to read "." as a word. Im not entirely sure how to stop this from happening without a use of another array. IF anyway has any advice itd be much appricated. thanks guys!
import java.util.*; public class wordCount { public static void main(String[] args) { final int LINES = 6; String [] chars = new String[50]; 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("The string input was: " + paragraph); System.out.println("The number of characters in this string is: " + (paragraph.length()-7)); String word = ""; int wordCount = 0; for (int i = 0; i < paragraph.length()-1;i += 1) { if (paragraph.charAt(i) != ' ') { word = ""; if((paragraph.charAt(i+1) == ' ')||(paragraph.charAt(i) == '.')||(paragraph.charAt(i) == ',')) { System.out.print(word); wordCount ++; word = ""; } } } for (int i=0; i<wordCount;i++) { if(chars[i]==(chars[i+1])) { wordCount=wordCount-1; i++; } } wordCount ++; System.out.println("The number of words: " + wordCount); } }