Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Why did this program cause Runtime Error? About problem #1002

Posted by takus at 2011-04-02 04:05:46 and last updated at 2011-04-02 04:06:21
I tried to solve No.1002 and wrote the following code. when i submit the code, the result was Runtime error. This program outputs exact result when i input Sample Input in (http://poj.org/problem?id=1002). Why did the program cause Runtime error? 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class Main {
	private static final char[] table =
        // A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,
        { '2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6',
        // P, ,R,S,T,U,V,W,X,Y
          '7', '7', '7', '7', '8', '8', '8', '9', '9', '9'
        };
	
	private static final HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
	
	private static final Comparator<Map.Entry<Integer, Integer>> comparator = new Comparator<Map.Entry<Integer, Integer>>() {
        public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
            return o1.getKey().compareTo(o2.getKey());
        }
    };
	
	public static void main(String[] args) {
		int n = 0;
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String str = null;
		try {
			str = in.readLine();
			n = Integer.parseInt(str);
		}
		catch (IOException e) {
			System.err.println("Error: I/O.");
			System.exit(1);
		}
		catch (NumberFormatException e) {
			System.err.println("Number format is invalid.");
			System.exit(1);
		}
		
		for(int i = 0; i < n; i++) {
			Integer number = null;
			
			try {
				if((str = in.readLine()) == null) break;
				number = translatePhoneNumber(str);
			}
			catch (IOException e) {
				System.err.println("Error: I/O.");
				System.exit(1);
			}
			catch (NumberFormatException e) {
				System.err.println("Number format is invalid.");
				continue;
			}
			increamentMap(number);
		}
		
        ArrayList<Map.Entry<Integer, Integer>> entries = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());
                
        Collections.sort(entries, comparator);
        
        if(entries.size() == 0 || entries.get(0).getValue() == 1) {
        	System.out.println("No duplicates.");
        }
        else {
	        for(int i = 0; i < entries.size(); i++) {
	        	String key = entries.get(i).getKey().toString();
	        	String number = key.substring(0, 3) + "-" + key.substring(3);
	        	int freq = entries.get(i).getValue();
	        	if(freq > 1) {
	        		System.out.println(number + " " + freq);
	        	}
	        }
        }
	}
	
	private static void increamentMap(Integer key) {
		if(map.containsKey(key)) {
			int num = map.get(key);
			map.put(key, num + 1);
		}
		else {
			map.put(key, 1);
		}
	}
	
	private static Integer translatePhoneNumber(String str) {
		int length = str.length();
		char[] chars = str.toCharArray();
		StringBuffer buf = new StringBuffer();
		for(int i = 0; i < length; i++) {
			if(chars[i] == '-') {
				continue;
			}
			if ('0' <= chars[i] && chars[i] <= '9') {
                buf.append(chars[i]);
                continue;
            }
            if ('A' <= chars[i] && chars[i] <= 'Z') {
				int d = chars[i] - 'A';
				buf.append(table[d]);
            }
		}
		if(buf.length() != 7) {
			throw new NumberFormatException();
		}
		
		return Integer.parseInt(buf.toString());
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator