Results 1 to 2 of 2
  1. #1
    stinson is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Word Count That Ignores Punctuation And Space

    Hi guys. I've spent a really long time writing this program and I'm incredibly stuck. The question is:

    Write a program that takes a string containing a sentence or a set of sentences, and counts the number of words in the sentence that meet or exceed a specified minimum length (in letters). For example, if the minimum length entered is 4, your program should only count words that are at least 4 letters long.

    Input the string and the minimum word length (integer), in that order, and output the word count (integer). Words will be separated by one or more spaces. Non-letter characters (spaces, punctuation, digits, etc.) may be present, but should not count towards the length of words.

    Hint: write a method that counts the number of letters (and ignores punctuation) in a string that holds a single word without spaces. In your main program, break the input string up into words and send each one to your method.

    The code I've written so far is
    Java Code:
    public class WordCount {
    	public static void main(String[] args) {
    
    		System.out.println("Enter string: ");
    		String str = IO.readString();
    		System.out.println("Enter minimum length: ");
    		int length = IO.readInt();
    		int wordcount = str.split("\\s+").length;
    		int charcounter = 0;
    		int finalcount = 0;
    		
    		
    		String[] wordcount2 = str.split("\\s+");
    
    		for (int i = 0; i < str.length(); i++) {
    			boolean isLetter = Character.isLetter(str.charAt(i));
    			if (isLetter) {
    				charcounter++;
    				if(str.length() >= length){
    					finalcount++;
    				}
    					
    			}
    
    
    		}
    
    		System.out.println(wordcount);
    		System.out.println(charcounter);
    		System.out.println(finalcount);
    	}
    
    }
    The problem I'm having is printing out the right "finalcount"
    If i input: cat dog elephant
    and
    minimum value 4
    the output should be 3 words, 14 characters, 1 word.
    However for finalcount, i'm getting 14.

    What can I do to compare each WORD in the string to the inputted length?

    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Word Count That Ignores Punctuation And Space

    Cross posted and much discussed before this was posted here:
    Word Count That Ignores Punctuation And Space - Java | Dream.In.Code

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Word Count in Java
    By michaelangelokrk in forum New To Java
    Replies: 4
    Last Post: 09-22-2011, 08:34 AM
  2. Replies: 3
    Last Post: 11-09-2010, 12:10 AM
  3. Count same word from many file in directory
    By cassiests in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 09:21 AM
  4. Count lines cointaining "word" in input file
    By gwithey in forum New To Java
    Replies: 5
    Last Post: 04-02-2009, 05:23 AM
  5. count occurence of word in a line of text
    By sinyi88 in forum New To Java
    Replies: 19
    Last Post: 02-28-2009, 07:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •