Results 1 to 3 of 3
  1. #1
    Alistair is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default [SOLVED] Help on Word and Character counting in java

    I need to know what I'm doing wrong here..

    Here is my objective:

    Write a program to read a sentence (a String) entered by the user and display the following statistics:
    1. the number of words in the sentence;
    2. the length of each word (the number of characters);
    3. the length of the longest word; and
    4. the length of the shortest word
    5. assume that the phrase will always end with punctuation and the punctuation will not be counted as a character in the computation of the length.

    example:


    Please enter the sentence to analyse:

    Assignment2 may require a loop!

    Here are some statistics on your sentence:
    Word 1 has 11 characters.
    Word 2 has 3 characters.
    Word 3 has 7 characters.
    Word 4 has 1 character.
    Word 5 has 4 characters.
    There are 5 words.
    The longest word has 11 characters.
    The shortest word has 1 character.
    Press any key to continue...




    Now I was able to code enough to find the amount of words in a string, but I'm stuck when I try to compute the length of each word :(

    If anybody can help, it would be greatly appreciated

    Oh and I CANNOT use StringTokenizer :(


    My code:


    Java Code:
    import java.util.Scanner;
    
    public class Assignment_2 
    {
    
        public static void main (String args[]) 
        	
        {   
        	
            Scanner userInput = new Scanner(System.in);
        	
        	String a = "";
        	
        	a = userInput.nextLine();
        	
    	  int textLength = a.length();
    	  int numberOfWords = 0;
    	  int countChar = 0;
    	  boolean isWord = false;
    
    		for (int i=0;i<textLength;i++)
    		{
    		   if (a.charAt(i) == ' ' || a.charAt(i) == '.'|| a.charAt(i) == '?' || a.charAt(i) == '!')
    		   {      
    			  numberOfWords++; \\when a space or a punctuation is found, increment number of words by one
    			  isWord = true; \\ flag isWord as true when ending of a word is found
    		   }
    			
    			else 			
    			{	
    			  countChar++; \\ otherwise we are inside a word and we need to increment the character count
    			
    			}
    			
    		    \\ I tried to put a while (isTrue) statement here, but it just  loops on forever.
    			
    		}
    			System.out.println("Word " + numberOfWords); 
        	}
        }

  2. #2
    Alistair is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default

    nevermind i figured it out :)

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    You can also use StringTokenizer....

    Maybe it would simplifies the operation about counting every length of words...
    freedom exists in the world of ideas

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 03:03 PM
  2. Replies: 13
    Last Post: 01-09-2008, 06:51 AM
  3. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 05:51 PM
  4. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 05:45 PM
  5. How to update Existing Word Doc Using java
    By umashankar in forum Advanced Java
    Replies: 1
    Last Post: 07-27-2007, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •