Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Word count, coutns "." as a word

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Word count, coutns "." as a word

    hey guys , Im also working on this word coiunt program and it seems to read "." as a word. Im not entirely sure how to stop this from happening without a use of another array. IF anyway has any advice itd be much appricated. thanks guys!
    import java.util.*;
     
    public class wordCount
    {
            public static void main(String[] args)
            {
                    final int LINES = 6;
                    String [] chars = new String[50];
                    Scanner in = new Scanner(System.in);
                    String paragraph = " ";
                    System.out.println("Please input " + LINES + " lines of text.");
                    for (int i = 0; i < LINES; i +=1)
                    {
     
                            paragraph = paragraph + " " + in.nextLine();
                    }
                    System.out.println("The string input was: " + paragraph);
                    System.out.println("The number of characters in this string is: " + (paragraph.length()-7));
     
                    String word = "";
                    int wordCount = 0;
                    for (int i = 0; i < paragraph.length()-1;i += 1)
                    {
     
                            if (paragraph.charAt(i) != ' ')
                            {
                                word = "";
                            if((paragraph.charAt(i+1) == ' ')||(paragraph.charAt(i) == '.')||(paragraph.charAt(i) == ','))
                            {
                                System.out.print(word);
                                wordCount ++;
                                word = "";
     
                            }
     
                            }
                    }
                    for (int i=0; i<wordCount;i++)
                    {
                        if(chars[i]==(chars[i+1]))
                        {
                            wordCount=wordCount-1;
                            i++;
                        }
                    }
                    wordCount ++;
                    System.out.println("The number of words: " + wordCount);
     
            }      
    }
    Last edited by jps; July 20th, 2013 at 10:53 PM. Reason: added code tags


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Word count, coutns "." as a word

    I don't think you've stated the problem correctly. Here's my sample run:
    Please input 6 lines of text.
    This is the first line.
    This is the second line.
    this is the third line.
    this is the fourth line.
    this is the fifth line.
    this is the sixth line.
    The string input was:   This is the first line. This is the second line. this is the third line. this is the fourth line. this is the fifth line. this is the sixth line.
    The number of characters in this string is: 140
    The number of words: 20
    What do you need help with?

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Word count, coutns "." as a word

    if the user inputs just a "." where :the firstline" , "second line" , "third line" etc is , the program reads the "." as a word and its not soppose to read punctionation as a word

Similar Threads

  1. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  2. word count in a in a file
    By raj4322 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 20th, 2012, 09:54 AM
  3. Count number of 'coin'p coins in a "machine"
    By BITmixit in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 20th, 2012, 04:34 PM
  4. the word "this"
    By vendetta in forum Java Theory & Questions
    Replies: 1
    Last Post: February 26th, 2010, 02:11 AM
  5. Reserved word "throws"
    By Lil_Aziz1 in forum Exceptions
    Replies: 1
    Last Post: January 1st, 2010, 01:12 AM