2017-04-27 43 views
0
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"); 
} 
+0

你不改变'input'。 – 4castle

+0

如果您使用的是Java 7+,则可能需要使用[用于在同一catch子句中捕获多个异常类型的语法](http://stackoverflow.com/q/3495926/5743988)。 – 4castle

回答

2

重塑你的循环,使用户被提示输入新的输入循环:

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