(UVA) Find the Telephone - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=738&page=show_problem&problem=1862


import java.io.*;
import java.util.*;

class Main {
    public Map<Character, Integer> map;

    public void initMap() {
        map.put('A', 2);
        map.put('B', 2);
        map.put('C', 2);
        map.put('D', 3);
        map.put('E', 3);
        map.put('F', 3);
        map.put('G', 4);
        map.put('H', 4);
        map.put('I', 4);
        map.put('J', 5);
        map.put('K', 5);
        map.put('L', 5);
        map.put('M', 6);
        map.put('N', 6);
        map.put('O', 6);
        map.put('P', 7);
        map.put('Q', 7);
        map.put('R', 7);
        map.put('S', 7);
        map.put('T', 8);
        map.put('U', 8);
        map.put('V', 8);
        map.put('W', 9);
        map.put('X', 9);
        map.put('Y', 9);
        map.put('Z', 9);
    }
   
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
       
        map = new HashMap<>();
        initMap();
       
        String line = br.readLine();
        while (line != null) {
            for (int i = 0; i < line.length(); i++) {
                char c = line.charAt(i);
                if (map.containsKey(c)) {
                    bw.write(String.valueOf(map.get(c)));
                }
                else {
                    bw.write(String.valueOf(c));
                }
            }
            bw.write("\n");
           
            line = br.readLine();
        }
                                              
        bw.flush();
        bw.close();
       
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução