Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-14-2008, 09:45 PM
Member
 
Join Date: May 2008
Posts: 2
Alistair is on a distinguished road
[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:


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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-14-2008, 11:58 PM
Member
 
Join Date: May 2008
Posts: 2
Alistair is on a distinguished road
nevermind i figured it out
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-15-2008, 05:48 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
You can also use StringTokenizer....

Maybe it would simplifies the operation about counting every length of words...
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
copying file from a email/word to a Java application cmbl Advanced Java 13 01-09-2008 08:51 AM
Counting numbers up and down radio New To Java 3 12-01-2007 09:08 PM
Counting Pixels shaungoater Java 2D 5 11-29-2007 07:51 PM
Counting Vowels and Constonants MattN New To Java 3 11-20-2007 07:45 PM
How to update Existing Word Doc Using java umashankar Advanced Java 1 07-27-2007 03:29 PM


All times are GMT +3. The time now is 11:11 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org