这是我的代码,用于计算单词的长度:
public class WordCount {
public static void main (String args []) {
String text;
text = "Java";
System.out.println (text);
//Work out the length
String [] input = text.split(" ");
int MaxWordLength = 0;
int WordLength = 0;
for (int i = 0; i < input.length; i++)
{
MaxWordLength = input[i].length();
WordLength = MaxWordLength;
} //End of working out length
//Work out no. of words
int[] intWordCount = new int[WordLength + 1];
for(int i = 0; i < input.length; i++) {
intWordCount[input[i].length()]++; }
for (int i = 1; i < intWordCount.length; i++) {
System.out.println("There are " + intWordCount[i] + " words of length " + MaxWordLength);
}
}
}
我遇到的问题是,当它打印出单词的长度时,我会得到以下结果:
Java
There are 0 words of length 4
There are 0 words of length 4
There are 0 words of length 4
There are 1 words of length 4
但是当我将文本更改为“J”时,会打印出来:
J
There are 1 words of length 1
知道为什么会这样做吗? 附:我是Java的新手,任何帮助都会受到赞赏。
答案 0 :(得分:1)
让我们来看看:
public class WordCount {
public static void main (String args []) {
String text;
text = "Java";
text
等于"Java"
。
System.out.println (text);
打印"Java"
//Work out the length
String [] input = text.split(" ");
这会将字符串"Java"
拆分为空格,其中没有。因此input
(我建议将其重命名为更具指示性的内容,如inputs
)等于一个元素的数组,并且该元素等于"Java"
。
int MaxWordLength = 0;
int WordLength = 0;
for (int i = 0; i < input.length; i++)
{
MaxWordLength = input[i].length();
对于每个元素,只有一个,MaxWordLength
被设置为第一个(也是唯一的)元素的长度,即"Java"
...长度为4。
WordLength = MaxWordLength;
所以WordLength
现在等于4
。
} //End of working out length
//Work out no. of words
int[] intWordCount = new int[WordLength + 1];
这将创建一个[WordLength + 1]
元素的int数组(等于[4 + 1]
或5
),其中每个元素都初始化为零。
for(int i = 0; i < input.length; i++) {
intWordCount[input[i].length()]++; }
对于input
中的每个元素,只有一个,这会设置input[i].length()
- th元素 - 第五个元素,因为input[i]
是"Java"
,它的长度为4 - 自身加一(因为++
)。
因此,在此for循环之后,数组现在等于[0, 0, 0, 0, 1]
。
for (int i = 1; i < intWordCount.length; i++) {
System.out.println("There are " + intWordCount[i] + " words of length " + MaxWordLength);
因此,这会自然地打印出不需要的输出。
}
}
}
输入仅为"J"
时输出不同,因为intWordCount
数组缩短为input[i].length()
个元素,现在为1.但值仍然设置为“本身加一个”,“自身”初始化为零(如all int-array elements are),然后加1(带++
)。
答案 1 :(得分:1)
我不确定您是否要计算字母或单词,因为您的代码会收到我的信件。
只需要从
更改此行String [] input = text.split(" ");
到
String [] input = text.split("");
并且您的计划完美无缺。
input: Java
output: There are 4 letters of length 1 <- Hope this is the expected result for you
使用 Lambda in Java
,您可以更好地减少头痛代码:
import java.util.*;
public class LambdaTest
{
public static void main (String[] args)
{
String[] st = "Hello".split("");
Collection myList = Arrays.asList(st);
System.out.println("your word has " + myList.stream().count() + "letters");
}
}
输出:
当你清除了你的问题时我的答案
代码:
public class WordCount
{
public static void main (String[] args)
{
String text ="";
int wordLenght = 0;
text = "Java is awesome for Me";
System.out.println (text);
String [] input = text.split(" ");
List<Integer> list = new ArrayList<>();
for (int i = 0; i < input.length; i++)
{
list.add(input[i].length());
}
Set<Integer> unique = new HashSet<Integer>(list);
for (Integer length : unique) {
System.out.println("There are " + Collections.frequency(list, length) + " words of length " + length);
}
}
}
输出:
There are 2 words of length 2
There are 1 words of length 3
There are 1 words of length 4
There are 1 words of length 7
注意:阅读关于HashSet和Set in Java
来源:http://javarevisited.blogspot.com/2012/06/hashset-in-java-10-examples-programs.html
答案 2 :(得分:0)
for (int i = 1; i < intWordCount.length; i++) {
System.out.println("There are " + intWordCount[i] + " words of length " + MaxWordLength);
}
1)你用intWordCount [i] == 0打印出单词,这就是为什么你有“有0个长度为X的单词”的原因
2)System.out.println("There are " ... + MaxWordLength);
应该是System.out.println("There are " ... + i);
,所以你有“有0个长度为1的单词”,“有0个长度为2的单词”,等等
答案 3 :(得分:0)
我知道这个问题很久以前就已经解决了,但是这里是另一个使用Java 8新功能的解决方案。使用Java流可以将整个练习写成一行:
Arrays.asList(new String[]{"Java my love"}) //start with a list containing 1 string item
.stream() //start the stream
.flatMap(x -> Stream.of(x.split(" "))) //split the string into words
.map((String x) -> x.length()) //compute the length of each word
.sorted((Integer x, Integer y) -> x-y) //sort words length (not necessary)
.collect(Collectors.groupingBy(x -> x, Collectors.counting())) //this is tricky: collect results to a map: word length -> count
.forEach((x,y) -> {System.out.println("There are " + y + " word(s) with " + x + " letter(s)");}); //now print each result
可能在几年时间内,这将成为解决此类问题的首选方法。无论如何,值得知道存在这样的替代方案。
答案 4 :(得分:-1)
Idk,但是使用长度方法就像你必须弄清楚长度机制是如何工作的那样就像使用单词定义一个单词一样。这是一个光荣的征服,弄清楚长度方法是如何工作的,但你应该避免使用长度方法。