You are on page 1of 5

Sentence count

Posted 31 March 2009 - 10:31 PM

Hi everyone-my first question on dreamincode

i had to create a program that asks user to input sentences and then displays the
number of sentences.

Example: user input: i like clouds. i hate rocks.


output: the no. of sentences are 2

so i worked on the program and this is what the main part of my code looks like:
{
c = new Console ();

c.println ("Please enter the sentences");


String userInput;
userInput=c.readLine ();

String word;
int punctuation=userInput.length;

for (int counter=0; counter<=1; counter++)


{

if(userInput.equals ("?") && userInput.equals ("!") && userInput.equals ("."))

{
c.println ("the number of sentences are");
c.println ((counter+userInput.length ()));

the problem: after running it and entering a sentence the code stops.
i need help with correcting the part in my for loop.
i will appreciate any help
Thanks alot! 

Word count

import java.io.*;

public class WordCount{

private static void linecount(String fName, BufferedReader in) throws IOException{

long numChar = 0;

long numLine=0;

long numWords = 0;

String line;

do{

line = in.readLine();

if (line != null){

numChar += line.length();

numWords += wordcount(line);

numLine++;

}while(line != null);

System.out.println("File Name: " + fName);

System.out.println("Number of characters: " + numChar);

System.out.println("Number of words: " + numWords);

System.out.println("Number of Lines: " + numLine);

}
private static void linecount(String fileName){

BufferedReader in = null;

try{

FileReader fileReader = new FileReader(fileName);

in = new BufferedReader(fileReader);

linecount(fileName,in);

catch(IOException e){

e.printStackTrace();

private static long wordcount(String line){

long numWords = 0;

int index = 0;

boolean prevWhiteSpace = true;

while(index < line.length()){

char c = line.charAt(index++);

boolean currWhiteSpace = Character.isWhitespace(c);

if(prevWhiteSpace && !currWhiteSpace){

numWords++;

prevWhiteSpace = currWhiteSpace;

return numWords;

}
public static void main(String[] args){

long numChar = 0;

long numLine=0;

String line;

try{

if (args.length == 0)

BufferedReader in = new BufferedReader(new


InputStreamReader(System.in));

line = in.readLine();

numChar = line.length();

if (numChar != 0){

numLine=1;

System.out.println("Number of characters: " + numChar);

System.out.println("Number of words: " + wordcount(line));

System.out.println("Number of lines: " + numLine);

}else{

for(int i = 0; i < args.length; i++){

linecount(args[i]);

catch(IOException e){

e.printStackTrace();

}
}

You might also like