14 Replies - 6918 Views - Last Post: 06 March 2012 - 02:55 AM Rate Topic: -----

#1 stinson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-February 12

Word count that ignores punctuation and space

Posted 05 March 2012 - 10:49 AM

Hi guys. I was wondering if you could help me out with some trouble I'm having. 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.



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 counter = 0;

		boolean isletter;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == ' ' || str.charAt(i) == '!') {
				isletter = false;
			}

			else {
				counter++;
			}
		}

		{

		}
	}
}




So far, I'm able to count the number of words (although I'm not really sure if I even need that for this problem) and the number of letters without ONE space. However, I have a few questions.

In this line,
 if (str.charAt(i) == ' ' || str.charAt(i) == '!') { 

How do I account for multiple spaces and other symbols? Such as if the user inputs: the cat !?@ dog.

Another question I have is how am I supposed to compare the length of EACH word to the minimum length the user inputted?

Any help would be appreciated. I'm just not sure how to get around these issues.

Is This A Good Question/Topic? 0
  • +

Replies To: Word count that ignores punctuation and space

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 10:57 AM

the Character class has a isLetter() method so:

isLetter = Character.isLetter(str.charAt(i));
Was This Post Helpful? 0
  • +
  • -

#3 stinson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-February 12

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 11:11 AM

View Postpbl, on 05 March 2012 - 10:57 AM, said:

the Character class has a isLetter() method so:

isLetter = Character.isLetter(str.charAt(i));


I tried what you said but I obviously didn't do it right since now I'm getting the wrong output.


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 counter = 0;

	for (int i = 0; i <str.length(); i++){
	boolean isLetter = Character.isLetter(str.charAt(i)); 
		isLetter = true;
		counter++;
		}

		System.out.println(wordcount);
		System.out.println(counter);
}
	}


If I input: cat dog
The output is 2 words and 7 characters when what I really need is 2 words and 6 characters.

Also, do you have any advice on how to measure the length of each word and compare it to the user inputted minimum length?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 11:21 AM

for (int i = 0; i <str.length(); i++){  
     boolean isLetter = Character.isLetter(str.charAt(i));   
     isLetter = true;  
     counter++;  
}  


Whatever the value returneed by Character.isLetter() is you overload it
isLetter = true;
and you systematically
counter++;


better to simply write: counter = str.length()

you sure you don't want: if(isLetter) counter++;
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 11:24 AM

Use a StreamTokenizer - it's already designed to distinguish words from noise like punctuation and spaces
Was This Post Helpful? 0
  • +
  • -

#6 stinson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-February 12

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 11:52 AM

View Postg00se, on 05 March 2012 - 11:24 AM, said:

Use a StreamTokenizer - it's already designed to distinguish words from noise like punctuation and spaces


I would if I had a remote idea what it was/if I would be allowed to use it for this. But I dont think I can :/

View Postpbl, on 05 March 2012 - 11:21 AM, said:

for (int i = 0; i <str.length(); i++){  
     boolean isLetter = Character.isLetter(str.charAt(i));   
     isLetter = true;  
     counter++;  
}  


Whatever the value returneed by Character.isLetter() is you overload it
isLetter = true;
and you systematically
counter++;


better to simply write: counter = str.length()

you sure you don't want: if(isLetter) counter++;


The output is still giving me 7 for "cat dog" instead of 6 for some reason
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 12:02 PM

did you replace
isLetter = true;

by
if(isLetter)
Was This Post Helpful? 0
  • +
  • -

#8 stinson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-February 12

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 12:14 PM

View Postpbl, on 05 March 2012 - 12:02 PM, said:

did you replace
isLetter = true;

by
if(isLetter)


I finally got it. I didn't make counter = str.length(); because that was still giving me 7. instead i did

int counter =0;

	for (int i = 0; i <str.length(); i++){
	boolean isLetter = Character.isLetter(str.charAt(i)); 
	if(isLetter){
		counter++;
	}


Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 12:17 PM

Quote

better to simply write: counter = str.length()

That was sarcastic :) I was simply telling you what your loop, whithout the if, was actually doing
Was This Post Helpful? 0
  • +
  • -

#10 stinson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-February 12

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 12:26 PM

View Postpbl, on 05 March 2012 - 12:17 PM, said:

Quote

better to simply write: counter = str.length()

That was sarcastic :) I was simply telling you what your loop, whithout the if, was actually doing


Oh wow my bad haha. Like I said before I'm kind of really new at this so I'm not sure what is right and wrong. If you don't mind me asking, could you help with another thing?
So right now my code is


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;

		for (int i = 0; i < str.length(); i++) {
			boolean isLetter = Character.isLetter(str.charAt(i));
			if (isLetter) {
				charcounter++;
			}

			for (int a = 0; a < str.length(); a++) {
				if ((str.length()) >= length) {
					finalcount++;
				}
			}
		}

		System.out.println(wordcount);
		System.out.println(charcounter);
		System.out.println(finalcount);
	}

}



I need to compare the string that the user inputs (such as: cat dog elephant) to the minimum number they input (such as 4)
With my code right now, I'm getting 2 words, 14 characters, 256 for word count. The word count is actually supposed to be 1. Do you have any advice as to how I could fix this?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 12:30 PM

I would rather do it that way:

		int lengthToOutput = 5;
		String line = "This is a sentence with: punctuation and, other stuff";
		
		String[] token = line.split(" ");
		for(int i = 0; i < token.length; ++i) {
			int counter = 0;
			for(int j = 0; j < token[i].length(); ++j) {
				if(Character.isLetter(token[i].charAt(j))) {
					++counter;
					if(counter == lengthToOutput) {
						System.out.println(token[i]);
						break;
					}
				}
			}
		}


Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 01:46 PM

Quote

if I would be allowed to use it for this. But I dont think I can


I don't see what there is in the spec to stop you
Was This Post Helpful? 0
  • +
  • -

#13 stinson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-February 12

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 02:22 PM

View Postpbl, on 05 March 2012 - 12:30 PM, said:

I would rather do it that way:

		int lengthToOutput = 5;
		String line = "This is a sentence with: punctuation and, other stuff";
		
		String[] token = line.split(" ");
		for(int i = 0; i < token.length; ++i) {
			int counter = 0;
			for(int j = 0; j < token[i].length(); ++j) {
				if(Character.isLetter(token[i].charAt(j))) {
					++counter;
					if(counter == lengthToOutput) {
						System.out.println(token[i]);
						break;
					}
				}
			}
		}



Thanks for your help again. For some reason though all it is printing is the actual words. And it also messes with the character count.
I took some of your advice though and Eclipse gave me an error

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(wordcount2[i].length() >= length){
					finalcount++;
				}
					
			}


		}

		System.out.println(wordcount);
		System.out.println(charcounter);
		System.out.println(finalcount);
	}

}



What I entered was:

Quote

Enter string:
cat dog elephant
Enter minimum length:
3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at WordCount.main(WordCount.java:19)


How would I go about fixing this? Also I still don't understand how to get the program to read one word at a time to determine if that length is greater than or equal to the inputted length.
Was This Post Helpful? 0
  • +
  • -

#14 stinson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-February 12

Re: Word count that ignores punctuation and space

Posted 05 March 2012 - 05:03 PM

okay I changed the for loop to

for (int i = 0; i < str.length(); i++) {
			boolean isLetter = Character.isLetter(str.charAt(i));
			if (isLetter) {
				charcounter++;
				if(wordcount2.length >= length){
					finalcount++;
				}



I'm not getting the array error anymore but I'm still not getting the right answer...
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Word count that ignores punctuation and space

Posted 06 March 2012 - 02:55 AM

It would also be possible to use the approach mentioned here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1