我正在学习自动化面试的Java编码。问题很简单。
写入一个字符串中的单词显示次数。
我完美地写出了代码(或者我以为如此),但是我在终端中得到了一个奇怪的输出。
代码:
import java.util.HashMap;
import java.util.Map;
public class TimesAWordAppearsInString {
public static void main(String[] args) {
// TODO Auto-generated method stub
//step 1. Declare sentence to be used in this class
String str ="I am learning learning java java programming";
//step 2.Create HashMap here! To count the amount times a word appears in the sentence
Map<String,Integer> map=new HashMap<String,Integer>();
//step 2a Get the HashMap to Count from starting place
Integer count = 1;
//step3.Split the string to include the spaces by converting it into an ARRAY (arr)
String[] arr=str.split(" ");
//step 4. Using the for...IF....else loop count the number of words in this string
for(int i=0; i<arr.length;i++){
//4a. How many times the word occurs
//4a.1 starting the process of counting the number times a word
//appear in the array and its duplicates. If there is a duplicate words use ELSE!
if(!map.containsKey(arr[i])){
map.put(arr[i], count);
}else{
map.put(arr[i], map.get(arr[i])+1); //get instance of the duplicate and increase by 1 for occurrence
}
//5.printout of string output
//5.1. go into the String and printout the word
for(String x:map.keySet()){
System.out.println("The count for word :"+x+"="+map.get(x));
}
}
}
}
输出是奇怪的,不是我所期望的。
我期待的是:
Java单词数= 3 字数I = 1 计数学习= 2
等。得到的是下面显示的内容。再来一次。我做错了什么?
输出。
The count for word :I=1
The count for word :I=1
The count for word :am=1
The count for word :I=1
The count for word :learning=1
The count for word :am=1
The count for word :I=1
The count for word :learning=2
The count for word :am=1
The count for word :java=1
The count for word :I=1
The count for word :learning=2
The count for word :am=1
The count for word :java=2
The count for word :I=1
The count for word :learning=2
The count for word :am=1
The count for word :java=2
The count for word :I=1
The count for word :learning=2
The count for word :am=1
The count for word :programming=1
答案 0 :(得分:1)
您已将您的2nd for循环放在第一个循环中。更新如下:
import java.util.HashMap;
import java.util.Map;
public class TimesAWordAppearsInString {
public static void main(String[] args) {
// TODO Auto-generated method stub
//step 1. Declare sentence to be used in this class
String str ="I am learning learning java java programming";
//step 2.Create HashMap here! To count the amount times a word appears in the sentence
Map<String,Integer> map=new HashMap<String,Integer>();
//step 2a Get the HashMap to Count from starting place
Integer count = 1;
//step3.Split the string to include the spaces by converting it into an ARRAY (arr)
String[] arr=str.split(" ");
//step 4. Using the for...IF....else loop count the number of words in this string
for(int i=0; i<arr.length;i++){
//4a. How many times the word occurs
//4a.1 starting the process of counting the number times a word
//appear in the array and its duplicates. If there is a duplicate words use ELSE!
if(!map.containsKey(arr[i])){
map.put(arr[i], count);
}else{
map.put(arr[i], map.get(arr[i])+1); //get instance of the duplicate and increase by 1 for occurrence
}
}
//5.printout of string output
//5.1. go into the String and printout the word
for(String x:map.keySet()){
System.out.println("The count for word :"+x+"="+map.get(x));
}
}
}
答案 1 :(得分:1)
似乎您正在使用较大的for循环的每次迭代打印出哈希图的输出(如下所示的循环)以向地图添加计数。
for(String x:map.keySet()){
System.out.println("The count for word :"+x+"="+map.get(x));
}
尝试将其移至循环之外,您将看到所需的结果。