Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-18-2012, 09:55 PM   PM User | #1
dingling
New to the CF scene

 
Join Date: Jul 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
dingling is an unknown quantity at this point
Need help with method to prompt for a series of words (dialogue boxes) and average

The method should repeatedly ask the user to enter the next word until the user signals the end of the series of words by entering a "*".
The method should then calculate the average length of all the words (not including the "*") and then return the average as a double value.

public static double averageLength()
{
int count = 0;
boolean numberOfCharacters = 0;
String word = "";
while (!word.equals("*"))
{
word = OUDialog.request("Please enter next word");
numberOfCharacters = numberOfCharacters + word.length();
count++;
}
return numberOfCharacters / count;
}


For some reason it seems to include the "*" as a word and I only want "*" to end the while loop. Please help
dingling is offline   Reply With Quote
Old 07-18-2012, 10:12 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You're not doing anything to stop the * from being counted both length wise and count wise within the loop. You'll need to add an if in there to only handle the numberOfCharacters and the count if the word is not *.
Fou-Lu is offline   Reply With Quote
Old 07-18-2012, 10:28 PM   PM User | #3
dingling
New to the CF scene

 
Join Date: Jul 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
dingling is an unknown quantity at this point
thanks, fixed it now.
dingling is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:16 AM.


Advertisement
Log in to turn off these ads.