TheDingo
TheDingo

Reputation: 59

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"); 
}

Upvotes: 0

Views: 49

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521103

Restructure your loop so that the user is prompted for new input inside the loop:

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

Upvotes: 2

Related Questions