英文:
why loop is not breaking here?
问题
class hashmaps {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
LinkedHashMap<String, Integer> hm = new LinkedHashMap<String, Integer>();
while (true) {
String a = s.next();
if (a.equals("") || a == null) {
break;
} else if (!hm.containsKey(a)) {
hm.put(a, 1);
} else {
hm.put(a, hm.get(a) + 1);
}
}
System.out.println(hm);
}
}
我试图从用户那里获取无限数量的值,并在用户输入空值或字符串时尝试打印存储在哈希映射中的值,但是当我在控制台中输入空值时循环没有中断。
英文:
class hashmaps{
public static void main(String args[]){
Scanner s = new Scanner(System.in);
LinkedHashMap<String,Integer> hm = new LinkedHashMap<String,Integer>();
while(true){
String a=s.next();
if(a.equals("") || a==null){
break;
}
else if(!hm.containsKey(a)){
hm.put(a,1);
}
else{
hm.put(a,hm.get(a)+1);
}
}
System.out.println(hm);
}
}
I'm trying to take infinite values from the user and trying to print the values stored in the hashmap when the user enters an empty value or string but the loop is not breaking when I enter an empty value in the console.
答案1
得分: 3
你需要使用 nextLine();
代替 next()
,后者等待获取非空内容,因此在遇到换行符时不会停止,而 nextLine
会停止。此外,
null
检查是无用的- 你可以使用
isEmpty
- 使用
merge
改进增量
<!-- -->
Scanner s = new Scanner(System.in);
LinkedHashMap<String, Integer> hm = new LinkedHashMap<>();
while (true) {
System.out.print("请输入一个单词:");
String a = s.nextLine();
if (a.isEmpty()) {
break;
}
hm.merge(a, 1, Integer::sum);
}
System.out.println(hm);
hm.merge(a, 1, Integer::sum);
的意思是
- 对于键
a
- 放入值
1
- 如果值已经存在,应用
Integer::sum
,相当于(prevVal, value) -> prevVal + value
。
英文:
You need to use nextLine();
instead of next()
which waits to get non-blank content, so it doesn't stop when it sees a newline unlike nextLine
. Also
- the
null
check is useless - you can use
isEmpty
- improve the increment with
merge
<!-- -->
Scanner s = new Scanner(System.in);
LinkedHashMap<String, Integer> hm = new LinkedHashMap<>();
while (true) {
System.out.print("Give a word: ");
String a = s.nextLine();
if (a.isEmpty()) {
break;
}
hm.merge(a, 1, Integer::sum);
}
System.out.println(hm);
The hm.merge(a, 1, Integer::sum);
means
- for key
a
- put the value
1
- if a value already exists, apply
Integer::sum
, same as(prevVal, value) -> prevVal + value
答案2
得分: 1
你需要使用 s.nextline() 替代 s.next(),请查看下面修改后的代码:
public class hashmaps {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
LinkedHashMap<String, Integer> hm = new LinkedHashMap<String, Integer>();
while (true) {
String a = s.nextLine();
if (a == null || a.equals("")) {
break;
} else if (!hm.containsKey(a)) {
hm.put(a, 1);
} else {
hm.put(a, hm.get(a) + 1);
}
}
System.out.println(hm);
}
}
英文:
You need to use s.nextline() instead of s.next(), check the modified code below:
public class hashmaps{
public static void main(String args[]){
Scanner s = new Scanner(System.in);
LinkedHashMap<String,Integer> hm = new LinkedHashMap<String,Integer>();
while(true){
String a=s.nextLine();
if(a==null || a.equals("")){
break;
}
else if(!hm.containsKey(a)){
hm.put(a,1);
}
else{
hm.put(a,hm.get(a)+1);
}
}
System.out.println(hm);
} }
答案3
得分: 0
其中一个主要问题是你正在使用s.next()
而不是s.nextLine()
:两种方法的区别在于,Scanner.next()
,我引用Java文档,
查找并返回扫描仪中的下一个完整标记。完整的标记前后都是与分隔符模式匹配的输入。该方法可能会阻塞,等待输入进行扫描,即使先前的hasNext()调用返回true。
而Scanner.nextLine()
则是,
将此扫描仪推进到当前行的末尾并返回被跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。位置设置为下一行的开头。
除此之外,在创建扫描器时,必须记住在完成后始终关闭它,因为如果不再使用它,可能会导致内存泄漏。另外,我强烈建议在需要比较两个字符串时使用equals()
方法,但在这种特定情况下,你还可以使用更实用的isEmpty()
方法。我建议你通过阅读关于这个话题的这个很棒的答案以及String文档来深入了解这个主题。
总之,代码应该像这样:
public static void main(String args[]){
Scanner s = new Scanner(System.in);
LinkedHashMap<String,Integer> hm = new LinkedHashMap<String,Integer>();
while(true){
String a=s.nextLine();
if(a.isEmpty())
break;
else if(!hm.containsKey(a))
hm.put(a,1);
else
hm.put(a,hm.get(a)+1);
}
s.close();
System.out.println(hm);
}
英文:
One of the main problem is that you're using s.next()
instead of s.nextLine()
: the difference between the two method is that Scanner.next()
, I quote Java documentation,
> Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.
Scanner.nextLine()
, instead,
>Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
Beyond that, when you create a scanner, you have to remind to always close it when you finished, because it cause a memory leak if you aren't using it anymore. Also, I strongly suggest to use, whenever you have to compare two strings, the equals()
method, but in this specific case you can also use the more practical isEmpty()
. I suggest you delve into the subject by reading this awesome answer on the topic and the String documentation.
In conclusion the code should be something like this:
public static void main(String args[]){
Scanner s = new Scanner(System.in);
LinkedHashMap<String,Integer> hm = new LinkedHashMap<String,Integer>();
while(true){
String a=s.nextLine();
if(a.isEmpty())
break;
else if(!hm.containsKey(a))
hm.put(a,1);
else
hm.put(a,hm.get(a)+1);
}
s.close();
System.out.println(hm);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论