Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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 *.

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, fixed it now.


  •  

    Posting Permissions

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