Reputation: 951
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++;
}
}
}
Upvotes: 0
Views: 419
Reputation: 3248
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);
}
}
}
Upvotes: 0
Reputation: 3436
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]);
}
}
Upvotes: 0
Reputation: 328608
Here are my suggestions:
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]);
}
}
}
Upvotes: 3
Reputation: 1879
Should the while loop not be
while(i < wordlist.Length){
if (wordlist[i] > 0){
System.out.println(i + "-letter words: " + wordlist[i]);
}
i++;
}
Upvotes: 1
Reputation: 4114
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;
}
Upvotes: 0