Results 1 to 4 of 4
  1. #1
    dingling is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with method to prompt for a series of words (dialogue boxes) and averag

    One possible solution is to include an if block inside of the while loop that first checks if the word equals "*" before processing it.

    You need to walk through the logic of your program. Pretend that you're the computer and that you're processing the steps of your code one line at a time, and think what happens when the user enters various Strings, and then finally enters, "*". This will show you why your current code *almost* works, and how to fix it. This is a valuable exercise to do whenever you're trying to figure out the logic of your code.

  3. #3
    dingling is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need help with method to prompt for a series of words (dialogue boxes) and averag

    Thank you. That worked. :)

    I'll remember the "one line at a time" :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with method to prompt for a series of words (dialogue boxes) and averag

    You're welcome and best of luck!

Similar Threads

  1. Urgent/ Help with average method in created class
    By johnjacob in forum New To Java
    Replies: 12
    Last Post: 12-07-2010, 03:47 AM
  2. Three Different Dialogue Boxes
    By lala in forum New To Java
    Replies: 7
    Last Post: 10-29-2010, 08:04 PM
  3. Replies: 10
    Last Post: 10-26-2010, 11:01 PM
  4. average using dialog boxes.
    By sarahhh in forum New To Java
    Replies: 1
    Last Post: 01-22-2010, 11:10 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
  •