gpt4 book ai didi

java - Try-Catch While 循环,Catch 不允许新输入?

转载 作者:行者123 更新时间:2023-12-02 02:52:08 26 4
gpt4 key购买 nike

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'
}
}

关于java - Try-Catch While 循环,Catch 不允许新输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43647030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com