• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Count Words

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've written a method to count the number of words in a string. I realize there is probably a method in the Java Documentation library that could make this short and sweet, but our teacher isn't allowing us to use it. I compiled my code and received the error "operator || cannot be applied to char, char" - here is my code:



How else can I specify in my if statement that if i = to any of the characters listed in the parentheses, i is not a letter? Please help - thanks!
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, you can't say "if ( a == 6 || 7 || 8 ) ..."

You must say "if ( a == 6 || a == 7 || a == 8 ) ..."
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you could do is

if(" \t\n.:;?!".indexOf(t.charAt(i)) != -1)

If you have any questions on what that does please post them, but if you look at the String API you should be able to figure it out.
 
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tip: If you try to write methods in such a way that it focuses on doing one thing, it will go a long way in making the problem easier to tackle and your code easier to write and read.

For example, your countWords method now does two things: it counts words and it checks if a character is a whitespace.

If you moved the logic that checks if a character is a whitespace to another method, you could write:

if (isWhiteSpace(ch)) {
...
} else {
...
}

This will tighten up the countWords method (formally, we say it makes the code more cohesive) and helps hide some of the implementation details that may be keeping you from seeing some of the flaws in your logic.

Here's another tip: the String class has a method that will return a char[]. You could iterate over this char[] instead of using the charAt(i) method.

Here's what your current logic reads like:



If you were given these instructions to perform, would it make sense to you?

One last tip: you are counting words, not whitespace. You need to keep track of when you're actually in a word. While you're in a word, watch out for whitespaces: this signals that you are done with the current word (now you can count it) and are either getting ready to start going through the next word or that there are no more words to count.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic