Results 1 to 7 of 7
- 04-25-2012, 06:41 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
I really need help explaining this Word Count program!
import java.util.*;
public class Count{
public static void main(String [] args){
String str;
Scanner s= new Scanner(System.in);
System.out.println("Enter String:");
str=s.nextLine();
int count=WCount(str);
System.out.println("Count="+count);
}
public static int WCount(String str)
{
int l=str.length();
int count=0;
for(int i=0;i<l;i++)
{
if((str.charAt(i)==' ')&&(i!=0)&&(str.charAt(i-1)!=' ')&&(i!=l-1))
count++;
}
if(count>0) count++;
return(count);
}
}
I just need to explain what each line does. This is part of my final and i really need help explaining this. Thank you!
- 04-25-2012, 06:44 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: I really need help explaining this Word Count program!
If it's part of your final, I suggest you try explaining it to us and we will tell you if you are close.
- 04-25-2012, 07:01 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Re: I really need help explaining this Word Count program!
I understand the first part, but the second method is where I'm completely lost. We never went over this in class and splitting a project into 2 methods. The most confusing thing for me is what's below
str=s.nextLine();
for(int i=0;i<l;i++)
{
if((str.charAt(i)==' ')&&(i!=0)&&(str.charAt(i-1)!=' ')&&(i!=l-1))
count++;
- 04-25-2012, 07:21 PM #4
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Re: I really need help explaining this Word Count program!
str = s.nextLine(); - this reads the next keyboard input and stores it in the string str that youd have declared.
for(int i=0;i<l;i++) - this will work through the length of the string which you got from int l=str.length();
The if statement then checks if the conditions you entered are correct, if so then you add 1 to count. The && means and so all conditions must be met.
If you created this program yourself you really should know what all the parts mean, or at least what you are trying to achieve by each line, otherwise it looks like you just copy and pasted it without any real knowledge of what your doing.
- 04-25-2012, 08:37 PM #5
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: I really need help explaining this Word Count program!
Why oh why oh why are you trying to answer it for him. Didn't you read what sunde887 wrote? The whole point is for the OP to make the initial effort!
- 04-25-2012, 08:50 PM #6
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Re: I really need help explaining this Word Count program!
Because its a simple problem and he clearly needs guideance. Yes if he did make this program himself he should know what all the elements in it are for, otherwise its highly suspicious as to whether he actually coded it himself or not. Im trying to give him a helping hand so hopefully next time he can tackle the problem himself and understand what he is doing. For a novice the simple things can often be what holds you back so a brief explanation wont do them any harm.
- 04-25-2012, 09:17 PM #7
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: I really need help explaining this Word Count program!
I wasn't the one who suggested it was his own program, I don't know that that is actually the case. I don't think that matters for the task at hand. In any case, yeah, you didn't give him the whole thing, so I'll recant. I just come from the school of ask questions to guide to an answer.
The task is simple enough that the OP could have shown some effort by posting his comments for the parts he claims to know. I certainly hope that he'll come back and show us that he's learning something based on what you've given him so far.
Similar Threads
-
Word Count That Ignores Punctuation And Space
By stinson in forum New To JavaReplies: 1Last Post: 03-06-2012, 04:20 AM -
Word Count in Java
By michaelangelokrk in forum New To JavaReplies: 4Last Post: 09-22-2011, 08:34 AM -
Count the frequency of the word in a text file instead of a sentence.
By bMorgan in forum New To JavaReplies: 3Last Post: 11-09-2010, 12:10 AM -
Count same word from many file in directory
By cassiests in forum New To JavaReplies: 4Last Post: 05-20-2010, 09:21 AM -
count occurence of word in a line of text
By sinyi88 in forum New To JavaReplies: 19Last Post: 02-28-2009, 07:37 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks