0

Looking for help with a basic array problem. Program has to read a sentence and store the frequencies of the word lengths in an array and then print out how many words are 1 letter words, 2 letter words etc.

I'm a pretty raw java programmer but have made a stab at it below would greatly appreciate some guidance. What I have seems to compile but spits out some garbled hex when I run the program and enter a sentence.

When I enter a sentence into the program I get an output like this:

[I@eb42cbf
[I@eb42cbf
[I@eb42cbf
[I@eb42cbf
[I@eb42cbf
[I@eb42cbf

My code:

class WordCount
{
    public static void main(String[] args)
    {
        int wordList[] = new int[20];
        System.out.println("Please enter a sentence.");

        for (int i = 0; i <= wordList.length; i++)
        {
            String s = Console.readToken();
            int x = s.length();
            wordList[x]++;
        }

        int x = 1;

        while (x < wordList.length)
        {
            if (wordList[x] > 0)
                System.out.println(x + "-letter words: " + wordList[x]);
            x++;
        }
    }
}
17
  • 1
    Smells like homework... Moreover, where does count come from? You did not define it. Maybe that should be wordList. Commented Jul 31, 2012 at 10:30
  • Afraid not, studying for an exam coming up next month using excercises from a book, no solutions or hints are provided unfortunately. I know im doing something silly wrong just cant spot it. Commented Jul 31, 2012 at 10:34
  • See my first comment. Additionally, the i in the second loop starts from 1. I think it should start from 0. Commented Jul 31, 2012 at 10:36
  • @Baz we can probably assume there is no 0-letter word ;-) Commented Jul 31, 2012 at 10:37
  • 1
    @Dave it makes no sense to force you to use his custom library when the JDK contains a class that does exactly the same thing. Anyway, your code works fine with a Scanner, so if what you posted is exactly what you run, the only possibility is that Console.readToken() does not do what you think it does. Commented Jul 31, 2012 at 11:00

5 Answers 5

3

Here are my suggestions:

  • use a Scanner instead of your Console.readToken, which is not part of the standard JDK
  • use standard variable names (i for a counter is better than x)
  • the rest of your code works fine
  • but I would use a for loop instead of the while because you know how many times you need to loop

Here is my version - The changes are: use of a scanner, renaming x into i and changing the while loop into a for loop:

public static void main(String[] args) {
    int wordList[] = new int[20];
    System.out.println("Please enter a sentence.");
    Scanner scanner = new Scanner(System.in); //Use a scanner, part of the standard JDK

    for (int i = 0; i <= wordList.length; i++) {
        String s = scanner.next(); //reads the next string
        int length = s.length();
        wordList[length]++;
    }

    for (int i = 0; i < wordList.length; i++) { //use a for loop
        if (wordList[i] > 0) {
            System.out.println(i + "-letter words: " + wordList[i]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Should the while loop not be

while(i < wordlist.Length){
  if (wordlist[i] > 0){
      System.out.println(i + "-letter words: " + wordlist[i]);
  }
  i++;      
}

3 Comments

That does not make sense: your proposal will be an infinite loop if there is a bucket with no words.
Corrected the i++, was trying to illustrate that the OP was using 'count[i]' instead of 'wordlist[i]'.
Apologies I changed the name of my array in an early version hence the discrepancy.
0
private static long wordcount(String line){
    long numWords = 0;
    int index = 0;
    boolean prevWhiteSpace = true;
    while(index < line.length()){
        char c = line.charAt(index++);
        boolean currWhiteSpace = Character.isWhitespace(c);
        if(prevWhiteSpace && !currWhiteSpace){
            numWords++;
        }
        prevWhiteSpace = currWhiteSpace;
    }
    return numWords;
}

2 Comments

I think what the OP needs is an explanation of what he is doing wrong rather than a solution...
Appreciate the solution but would benefit more from advice on where Im going wrong with my own answer. Thanks people.
0

Yet another approach :

int wordList[] = new int[20];
    System.out.println("Please enter a sentence.");
    String s = "";
    try{
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        s  = bufferRead.readLine();        
    }
    catch(IOException e){
        e.printStackTrace();
    }

    String[] s1 = s.split(" ");
    for(int i = 0 ; i < s1.length ; i++){
        int len = s1[i].length(); 
        wordList[len]++; 
    }
    for (int i = 0; i < wordList.length; i++) { //use a for loop
    if (wordList[i] > 0) {
        System.out.println(i + "-letter words: " + wordList[i]);
       }
    }

Comments

0

If you mean that when a sentence is entered like: "This is a sentence that", the output should be:

4-letter words: 2

2-letter words: 1

1-letter words: 1

8-letter words: 1

My Code:

  import java.util.HashMap;
  import java.util.Scanner;

        class WordCount {
            public static void main(String[] args) {
                HashMap<Integer, Integer> statistic = new HashMap<Integer, Integer>();

                System.out.println("Please enter a sentence.");
                Scanner in = new Scanner(System.in);
                String s = in.nextLine();

                String[] words = s.split(" ");

                for (int i = 0; i < words.length; i++) {
                    if (statistic.containsKey(words[i].length())) {
                        Integer value = statistic.get(words[i].length());
                        statistic.put(words[i].length(), value + 1);
                    } else
                        statistic.put(words[i].length(), 1);
                }

                for (Integer num : statistic.keySet()) {

                    Integer key = num;
                    Integer value = statistic.get(num);
                    System.out.println(key + "-letter words: " + value);

                }

            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.