[英]While loop with try-catch blocks doesn't wait for input in the try block after looping once
[英]Try-Catch While Loop, Catch won't allow new input?
try {
while ((inputLine = bufferedReader.readLine()) != null) {
String[] words = inputLine.split("[ \n\t\r.,;:!?(){}]");
for (int wordCounter = 0; wordCounter < words.length; wordCounter++) {
String key = words[wordCounter].toLowerCase();
if (key.length() > 0) {
if (map.get(key) == null) {
map.put(key, 1);
}
else {
int value = map.get(key).intValue();
value++;
map.put(key, value);
}
}
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {
System.out.println(entry.getValue() + "\t" + entry.getKey());
}
}
} catch (IOException error) {
System.out.println("Invalid File");
}
重组循环,以便在循环内提示用户输入新内容:
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
Scanner web = null;
while (true) {
try {
String input = scan.nextLine();
URL url = new URL(input);
web = new Scanner(url.openStream());
break;
} catch (MalformedURLException e) {
System.out.println("error occurred: " + e);
} catch (IOException e) {
System.out.println("error occurred: " + e);
}
}
if (web != null ) {
// do something with 'web'
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.