简体   繁体   中英

Basic array issue in Java

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++;
        }
    }
}

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]);
        }
    }
}

Should the while loop not be

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

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);

                }

            }
        }

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]);
       }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM