Results 1 to 7 of 7
  1. #1
    walsby14 is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default non working code :S

    public static double averageLength()
    {
    int count = 0; // to hold the number of words input
    int numberOfCharacters = 0; // to hold the total number of characters
    String word = "" ; // to hold each word as it is input
    {
    while (!word.equals("*")) // loop until the word input is "*"
    word = OUDialog.request("Please enter next word"); //input the next word
    numberOfCharacters = numberOfCharacters + (word.length()); // add its length onto the number of characters
    count++; // add 1 to the number of words
    }
    return numberOfCharacters / count; // calculate the average by diving the number of characters
    // by the number of words and return the average
    }

    here is my code, its meant to return the average number of letters from the words entered but i can't get it to return anything other than 1.0. without changing the whole thing to an if loop or something.

    any ideas??

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: non working code :S

    Ok first thing you should do is change
    Java Code:
    int count = 0;
    int numberOfCharacters = 0;
    to
    Java Code:
    double count = 0;
    double numberOfCharacters = 0;
    Since you are returning a double and i assume you would like the average to a decimal place.
    Next, can you give me an example of the inputs you are giving to OUDialog.request().
    (ie. the list of words you are inputting).

  3. #3
    walsby14 is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: non working code :S

    Quote Originally Posted by jhuber151 View Post
    Ok first thing you should do is change
    Java Code:
    int count = 0;
    int numberOfCharacters = 0;
    to
    Java Code:
    double count = 0;
    double numberOfCharacters = 0;
    Since you are returning a double and i assume you would like the average to a decimal place.
    Next, can you give me an example of the inputs you are giving to OUDialog.request().
    (ie. the list of words you are inputting).

    Ok made those changes, the inputs are just any words really, inputting one word such as "this" then pressing ok, inputting another word "that" and then * to finish.

  4. #4
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: non working code :S

    So its a little issue that you have with the code. You are missing brackets around the while loop.
    Java Code:
    while (!word.equals("*")) // loop until the word input is "*"
    { //<-- BRACKETS AROUND BODY OF LOOP
    word = OUDialog.request("Please enter next word"); //input the next word
    numberOfCharacters = numberOfCharacters + (word.length()); // add its length onto the number of characters
    count++; // add 1 to the number of words
    } //<-- BRACKETS AROUND BODY OF LOOP
    Without the brackets a loop statement or if statement only run the next line. Therefore only the request will run until you hit "*" and then it will only count the number of letters there which will be 1 for "*". After the brackets are placed in it works fine. Also you should put in a check so that the program doesn't count the "*" as a letter, since it is counting it in numberOfCharacters and in the word count.

  5. #5
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: non working code :S

    @world - You could do this by placing the names into an array or vector, then remove the last component of that vector
    Java Code:
    while(!word.equals("*"))
    { 
    //PLACE IN AN VECTOR
    }
    vector.remove(vector.size()-1); // THIS WILL REMOVE THE LAST COMPONENT OF THE VECTOR

  6. #6
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: non working code :S

    How are you storing the names?

  7. #7
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: non working code :S

    So after you read in the request do some kind of check like
    Java Code:
     
    if(!(name.equals("*")) 
    {
     //DO SOMETHING
    }

Similar Threads

  1. Code not working
    By usman400 in forum New To Java
    Replies: 4
    Last Post: 06-06-2012, 08:55 AM
  2. Replies: 1
    Last Post: 03-25-2012, 02:40 AM
  3. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  4. Why is the code not working
    By sanox in forum New To Java
    Replies: 12
    Last Post: 09-08-2009, 11:28 AM
  5. JNI: Why this code not working?
    By playwin2 in forum Advanced Java
    Replies: 5
    Last Post: 11-25-2008, 01:12 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
  •