Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sentence counter
04-03-2009, 01:04 AM
Post: #1
Sentence counter
Hi everyone-my first question on javacodez

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!
Find all posts by this user
Quote this message in a reply
04-03-2009, 01:24 AM
Post: #2
RE: Sentence counter
Try this loop

Code:
int punctuation=userInput.length();

        int sentenceCounter = 0;
        for (int counter=0; counter<punctuation; counter++)
        {
            if(userInput.charAt(counter) == '?' || userInput.charAt(counter) == '!' || userInput.charAt(counter) == '.')
            {
                sentenceCounter++;
            }
        }
        
        System.out.println("Number of sentences are :- "+sentenceCounter);
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: