Sunday, 15 May 2011

java - Can someone help me add a line counter to my code when I read this file -



java - Can someone help me add a line counter to my code when I read this file -

i'm not sure how add together line counter because if while statement such

while (filereader.hasnextline()) { lines+=1; file.nextline(); }

then rest of vowels, sentences, etc set 0.

my code is:

scanner input = new scanner(system. in ); system.out.println("enter file name: "); file file = new file(input.nextline()); if (file.length() == 0) { system.out.println("the input file empty."); system.exit(1); } scanner filereader = new scanner(file); while (filereader.hasnext()) { string word = filereader.next(); (int = 0; < word.length(); i++) { char ch = word.charat(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowels += 1; if ((ch == '!' || ch == '.' || ch == '?')) sentences += 1; if (character.isletterordigit(ch)) alphanumeric += 1; switch (ch) { case ',': punctuation += 1; break; case '[': punctuation += 1; break; case ']': punctuation += 1; break; case ':': punctuation += 1; break; case '`': punctuation += 1; break; case '-': punctuation += 1; break; case '!': punctuation += 1; break; case '_': punctuation += 1; break; case '(': punctuation += 1; break; case ')': punctuation += 1; break; case '.': punctuation += 1; break; case '?': punctuation += 1; break; case '"': punctuation += 1; break; case ';': punctuation += 1; break; } } words += 1; } system.out.println("the number of words in file name: " + words); system.out.println("the number of lines in file name: " + lines); system.out.println("the number of alphanumeric characters " + "in file name: " + alphanumeric); system.out.println("the number of sentences in file name: " + sentences); system.out.println("the number of vowels in file name: " + vowels); system.out.println("the number of punctuations in file name: " + punctuation);

newlines denoted character '\n'. check instances of that, same way checking vowels, punctuation, etc.

java

No comments:

Post a Comment