In need of some java help

Thunderbird

Newcomer
So I'm working on an assignment where I need to create a counter class that can compute the number of sentences in a paragraph. The paragraph is just some little thing like, " This is a sentence. This is too."
The point is to learn about while loops with if statements in them. Here's what I have so far:

public class Counter {

public Counter (String paragraph ){

s1 = paragraph;
}

public String getSentenceCount(){
int i = 0;
int sentence = 0;

while ( i < s1.length())
if (s1.charAt(i) .equals(".") || s1.charAt(i) .equals("!") || s1.charAt(i) .equals("?"))
sentence ++;

i ++;

return s1;
}

private String s1;
}


The error that I get three times is this, " line 13: char cannot be dereferenced" reffering to my i variable each time in the if statement. Can anyone clue me in?
 
Doesn't charAt(i), return the primitive char, which is nto a class, in which case you shouldn't be using .equals(), since it can't handle any messages whatsoever.
 
If you want to do it in true Java fashion (use what already exists), here's one solution to consider:

Code:
/**
 * Returns an estimated sentence count of the provided block of text.
 *
 * @param text The text block to peruse.
 *
 * @return The sentence count.
 */
public static final long getSentenceCount(final String paragraph)
{
    return new java.util.StringTokenizer(paragraph, ".!?").countTokens();
}

If you need to be exact, then you might consider switching to using regular expressions to ensure the decimal point is followed by a space, end quote, or end of string is reached. This is to rule out miscounts due to numbers such as $1219.37. Although the naive solution isn't too bad in this case.

What's fundamental to all programing languages, after one knows the syntax, is to become familar with the provided libraries. In this day and age, there's no excuse or time for reinventing the wheel.

EDIT: blasted code tag never seems to function as well as it should...
 
K.I.L.E.R said:
Here is what I cooked up in a few minutes:
http://members.optusnet.com.au/ksaho/Algorithm/JAVA/ds.java

Why not just break apart the string into characters and work with the character array which makes it tons easier to work with?

If you're going to be dealing with the String as an array of chars, save yourself and the computer a lot of work on the copy and extraneous bounds checking by creating the char array directly like this:

Code:
final char a[] = s1.toCharArray();
for(int charIdx = 0; charIdx < a.length; charIdx++)
{
     // ... main logic in here ...
}
 
Good ol' BRiT, I forgot about optimising my program.
I forgot about Tokenizers. :oops:

/me bends over for a spanking :p

Thunderbird, are you making a text editor or just a program for school/uni?
 
K.I.L.E.R said:
Thunderbird, are you making a text editor or just a program for school/uni?

It's just a basic program for university. Im just in the introductory comp sci class, and am not that great at programming, but I like it when everything works!
 
Back
Top