Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By sunde887

Thread: I really need help explaining this Word Count program!

  1. #1
    rsp0413 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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.
    jlczuk likes this.

  3. #3
    rsp0413 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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++;

  4. #4
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default 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.

  5. #5
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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!

  6. #6
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default 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.

  7. #7
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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

  1. Word Count That Ignores Punctuation And Space
    By stinson in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 05:20 AM
  2. Word Count in Java
    By michaelangelokrk in forum New To Java
    Replies: 4
    Last Post: 09-22-2011, 09:34 AM
  3. Replies: 3
    Last Post: 11-09-2010, 01:10 AM
  4. Count same word from many file in directory
    By cassiests in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 10:21 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, 08: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
  •