Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with counting short and long words

 
Alfonso Saballett
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Everybody

As you will see by the code I will post I'm trying to get a short and long count. I have been looking at this site and other sites I can't figure out how to do it. As you can guess by my coding I'm a total beginner and I'm still trying to understand what to use and when to use it, so please keep it basic. So here is what I have so far.

 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you to look at String#Split() method.

And Please use CODE tags to enclose your code.
 
Alfonso Saballett
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about not using the Code tags, I went to the link you gave me and I looked at other sites and so far this is what I understand what split() will do: breaks a string into substrings, or tokens. I can see how that would be useful but how do I tell it to recognize a short word (3 or 4 characters)? Could you provide an example close to what I need to do.
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code doesn't count the number of short words and long words. The short words variable is a count of the number of characters in the string, whose ASCII value is less than or equal to the ASCII value for the character "4". The long words variable is a count of the number of characters in the string, whose ASCII value is greater than or equal to the ASCII value for the character "5".

Henry
 
Alfonso Saballett
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I'm understanding you correctly if I run this program and I don't have a "4" or "5" in the input, it won't count it but when I run it by typing in, "This is a test. This is a test." I get the result of 9 shorts words and 22 long words obviously this is wrong but from what you are saying should it be 0 shorts words and 0 long words? I appreciate the help and patients. It was suggested earlier that I use the split() method, how?
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfonso Saballett wrote:So if I'm understanding you correctly if I run this program and I don't have a "4" or "5" in the input, it won't count it but when I run it by typing in, "This is a test. This is a test." I get the result of 9 shorts words and 22 long words obviously this is wrong but from what you are saying should it be 0 shorts words and 0 long words? I appreciate the help and patients. It was suggested earlier that I use the split() method, how?



Nope... here is an ASCII table...

http://www.asciitable.com/

Basically, all number digits less than 4, along with the spaces and other punctuations, will count as a short word. All number digits greater than 5, along with the alphabet will count as a long word. See the table for what counts as what.

To give more detail, your program is reporting 9 spaces and periods, and 22 letters.

Henry
 
Alfonso Saballett
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that link I see why putting a number there won't work, any other suggestions. I'm still trying to make it work with split() but can't figure out how to do it.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Lets first see how String#Split method works.



Run this and tell us do you find any difficulties understanding this code snippet?
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run this and tell us do you find any difficulties understanding this code snippet?



Just throwing a monkey wrench into this.... I am assuming that this is a homework problem, for a beginner's class. And as such, I am assuming the instructor is expecting you to loop through the characters of the string -- To determine what is a whitespace versus what is a letter. To determine whether you are in a word or not. To determine whether you are in a sentence or not... While keeping count of the size of the words too.

Using the regular expression engine to do this may not be allowed by your instructor.

Henry
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, Do you want me to remove that code snippet? OP has nowhere mentioned about Split method that it is not allowed?
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Pandya wrote:Henry, Do you want me to remove that code snippet? OP has nowhere mentioned about Split method that it is not allowed?


No. There is nothing wrong with the post. I just wanted to make sure that the OP don't use a library, that technically, haven't learn to use yet.

Henry
 
Alfonso Saballett
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys

Yes Henry, your assumptions are correct but I think it would be OK for to use it as an advance student use BreakIterator and that has not been cover but he was fine with it.

I ran the code snippet and it gave me the back what I had enter. I can use the split() method but I don't understand the following snippet of code:

I understand the for loop that I have used in my code but not this type. Thanks for the example it has help me understand the split() method better. I will try to figure out how I can use it to count the small and large words.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's an enhanced For loop introduced in Java 5. But you can continue with the traditional for loop that you are used to.

Now the next thing is to find short/long words. Are you aware about String#length?
 
Alfonso Saballett
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I'm aware of it and I have used it in my code, from my understanding of it returns the length of this string.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Let us know If you find any other difficulties.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic