3 Replies - 3494 Views - Last Post: 01 August 2014 - 06:49 AM Rate Topic: -----

#1 Alice.White   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-August 14

Stuck on average word length

Posted 01 August 2014 - 05:14 AM

Hello,
The goal of this code was to create a program using main method java to analysis a piece text which has been entered from a user.

They do this by entering the text into a scanner which is then analysed by the program. The analysis is to produce word frequency, for example " This is a test" produces this results:

This is a test
1 letter words: 1
2 letter words: 1
3 letter words: 0
4 letter words: 2
5 letter words: 0

The bit that I'm stuck on is producing a mean/average, My guts telling to divide
counts.length
by
str.length
but I'm not the Best at java and I've tried to implement this but all I get are errors. I'm not expecting anyone to just hand me code, but if someone could give me a hint in what I should do or just point me the right direction it would be greatly appreciated.

Thank you

-Alice

Code:
import java.util.Scanner;
	public class While_Loop { 

	public static void main (String[] args) {

while(true){

    Scanner input = new Scanner(System.in);

    System.out.println("Enter text: "); 
    String s;
    s = input.nextLine();
    System.out.println("" + s);

    String[] strings = s.split(" ");
    int[] counts = new int[6];
    for(String str : strings)
        if(str.length() < counts.length) counts[str.length()] += 1;
    for(int i = 1; i < counts.length; i++)
    	System.out.println(i + " letter words: " + counts[i]);	

        
    }}}


Is This A Good Question/Topic? 0
  • +

Replies To: Stuck on average word length

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Stuck on average word length

Posted 01 August 2014 - 06:32 AM

The simplest thing to do is to do the averaging before you increment the counts array. All you need do is just increment a running total of word lengths then divide by strings.length
Was This Post Helpful? 0
  • +
  • -

#3 Alice.White   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-August 14

Re: Stuck on average word length

Posted 01 August 2014 - 06:44 AM

Sorry forgot to update my post, I hit my head against the wall a few time and managed to create the mean.

Code below

import java.util.Scanner;
	public class While_Loop { 

	public static void main (String[] args) {
		
	Scanner input = new Scanner(System.in);
	while(true){
		
    System.out.println("Enter text: "); 
    String s;
    s = input.nextLine();
    System.out.println("" + s);

    String[] strings = s.split(" ");
    int[] counts = new int[6];
    for(String str : strings)
        if(str.length() < counts.length) counts[str.length()] += 1;
    int total = 0;
    for(String s1 : strings) total += s1.length();
    
    for(int i = 1; i < counts.length; i++)

    	System.out.println(i + " letter words: " + counts[i]);
    	System.out.println(("mean lenght: " ) + ((double) total/strings.length));

        
    }}}

Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Stuck on average word length

Posted 01 August 2014 - 06:49 AM

Well, as i mentioned above, you needn't suffer the inefficiency of iterating the array any more than once
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1