为什么循环没有在这里中断?
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);
}
}
我正在尝试从用户那里获取无限值,并尝试在用户输入空值或字符串时打印哈希图中存储的值,但当我在控制台中输入空值时循环没有中断.
您需要使用 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);
} }
您需要使用 nextLine();
而不是等待获取 non-blank 内容的 next()
,因此它不会像 nextLine
那样在看到换行符时停止。还有
null
检查没用
- 你可以使用
isEmpty
- 用
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);
hm.merge(a, 1, Integer::sum);
表示
- 键
a
- 输入值
1
- 如果值已经存在,应用
Integer::sum
,与(prevVal, value) -> prevVal + value
相同
主要问题之一是您使用的是 s.next()
而不是 s.nextLine()
:这两种方法的区别在于 Scanner.next()
,我引用 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()
,相反,
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.
除此之外,当您创建扫描仪时,您必须提醒您在完成后始终关闭它,因为如果您不再使用它会导致内存泄漏。此外,我强烈建议在必须比较两个字符串时使用 equals()
方法,但在这种特定情况下,您也可以使用更实用的 isEmpty()
方法。我建议您通过阅读 this awesome answer on the topic and the String documentation 深入研究该主题。
总之,代码应该是这样的:
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);
}
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);
}
}
我正在尝试从用户那里获取无限值,并尝试在用户输入空值或字符串时打印哈希图中存储的值,但当我在控制台中输入空值时循环没有中断.
您需要使用 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);
} }
您需要使用 nextLine();
而不是等待获取 non-blank 内容的 next()
,因此它不会像 nextLine
那样在看到换行符时停止。还有
null
检查没用- 你可以使用
isEmpty
- 用
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);
hm.merge(a, 1, Integer::sum);
表示
- 键
a
- 输入值
1
- 如果值已经存在,应用
Integer::sum
,与(prevVal, value) -> prevVal + value
相同
主要问题之一是您使用的是 s.next()
而不是 s.nextLine()
:这两种方法的区别在于 Scanner.next()
,我引用 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()
,相反,
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.
除此之外,当您创建扫描仪时,您必须提醒您在完成后始终关闭它,因为如果您不再使用它会导致内存泄漏。此外,我强烈建议在必须比较两个字符串时使用 equals()
方法,但在这种特定情况下,您也可以使用更实用的 isEmpty()
方法。我建议您通过阅读 this awesome answer on the topic and the String documentation 深入研究该主题。
总之,代码应该是这样的:
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);
}