#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Counting spaces or # of words


    Hi guys, I need help on calculating the average word length.

    // Counts number of the words
    numWords = 0; // Resets numWords Value
    stringScan = new Scanner(sentence);
    while(stringScan.hasNext())
    {
    word = stringScan.next();
    numWords++;
    totalWords++;

    }
    System.out.println("The number of words in this sentence is: " + numWords);

    // Calculates average word length
    wordLength = cleanString.length();
    System.out.println("Word Length: " + wordLength);
    avgWordLength = wordLength / totalWords;
    System.out.println("The average word length is: " + avgWordLength);
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you explain what your problem is? What does the program currently print out?
    Can you post its output and explain what is wrong with it?

    Are you having problems with integer arithmetic? 4/10 = 0 and 4.0/10 = 0.4

IMN logo majestic logo threadwatch logo seochat tools logo