java - How to take first word of new paragraph into consideration? -


i'm trying build program takes in files , outputs number of words in file. works when under 1 whole paragraph. however, when there multiple paragraphs, doesn't take account first word of new paragraph. example, if file reads "my name john" , program output "4 words". however, if file read"my name john" each word being new paragraph, program output "1 word". know must if statement, assumed there spaces before new paragraph take first word in new paragraph account. here code in general:

import java.io.*; public class helloworld {     public static void main(string[]args)     {         try{             // open file first             // command line parameter             fileinputstream fstream = new fileinputstream("health.txt");             // use datainputstream read binary not text.             bufferedreader br = new bufferedreader(new inputstreamreader(fstream));             string strline;              int word2 =0;             int word3 =0;             //read file line line             while ((strline = br.readline()) != null)   {                 // print content on console                 ;                 int wordlength = strline.length();                 system.out.println(strline);                 for(int = 0 ; < wordlength -1 ; i++)                     {                         character = strline.charat(i);                         character b= strline.charat(i + 1);                         **if(a == ' ' && b != '.' &&b != '?' && b != '!' && b != ' ' )**                             {                                 word2++;                                 //doesnt take account 1st character of new paragraph                             }                     }                 word3 = word2 + 1;             }                system.out.println("there " + word3 + " "                                + "words in file.");             //close input stream             in.close();         }catch (exception e){//catch exception if             system.err.println("error: " + e.getmessage());         }       } } 

i've tried adjusting if statement multiple teams, not seem make difference. know i'm messing up?

i'm pretty new user , asked similar question couple days people accusing me of demanding of users, narrows question bit. confused on why not taking account first word of new paragraph. please let me know if need more information. thanks!!

firstly, counting logic incorrect. consider:

word3 = word2 + 1; 

think does. every time through loop, when read line, count words in line, reset total count word2 + 1. hint: if want count total number in file, you'd want increment word3 each time, rather replace current line's word count.

secondly, word parsing logic off. consider case of blank line. see no words in it, treat word count in line word2 + 1, means incorrectly counting blank line 1 word. hint: if first character on line letter, line starts word.

your approach reasonable although implementation flawed. alternate option, may want consider string.split() on each line. number of elements in resulting array number of words on line.

by way, can increase readability of code, , make debugging easier, if use meaningful names variables (e.g. totalwords instead of word3).


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -