Super easy problem if you’re used to a bit of C++ and familiar to the printf method.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.StringTokenizer; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P11340 { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int N = Integer.parseInt(sc.readLine()); for (int zz = 0; zz < N; zz++) { int K = Integer.parseInt(sc.readLine()); HashMap<Character, Integer> map = new HashMap<Character, Integer>(); StringTokenizer st = null; for (int yy = 0; yy < K; yy++) { st = new StringTokenizer(sc.readLine()); map.put(st.nextToken().charAt(0), Integer.parseInt(st.nextToken())); } double ans = 0; int M = Integer.parseInt(sc.readLine()); for (int xx = 0; xx < M; xx++) { char[] line = sc.readLine().toCharArray(); for (Character c : line) { if (map.containsKey(c)) { ans += map.get(c); } } } ans /= 100; out.printf("%.2f$" + System.lineSeparator(), ans); } out.close(); sc.close(); } }