Problem:
Extraordinary Girl (II)

She loves e-mail so much! She sends e-mails by her cellular phone to
her friends when she has breakfast, she talks with other friends, and even
when she works in the library! Her cellular phone has somewhat simple
layout (Figure 1). Pushing button 1 once displays a character (’), pushing

it twice in series displays a character (,), and so on, and pushing it 6 times
displays (’) again. Button 2 corresponds to charaters (abcABC), and, for
example, pushing it four times displays (A). Button 3-9 have is similar to
button 1. Button 0 is a special button: pushing it once make her possible
to input characters in the same button in series. For example, she has to
push “20202” to display “aaa” and “660666” to display “no”. In addition,
pushing button 0 n times in series (n > 1) displays n − 1 spaces. She never
pushes button 0 at the very beginning of her input. Here are some examples
of her input and output:

666660666 --> No
44444416003334446633111 --> I’m fine.
20202202000333003330333 --> aaba f ff

One day, the chief librarian of the library got very angry with her and
hacked her cellular phone when she went to the second floor of the library
to return books in shelves. Now her cellular phone can only display button
numbers she pushes. Your task is to write a program to convert the sequence
of button numbers into correct characters and help her continue her e-mails!

Input

Input consists of several lines. Each line contains the sequence of button
numbers without any spaces. You may assume one line contains no more
than 10000 numbers. Input terminates with EOF.

Output

For each line of input, output the corresponding sequence of characters in
one line.

Sample Input

666660666
44444416003334446633111
20202202000333003330333

Output for the Sample Input

No
I'm fine.
aaba f ff

Solution in JAVA (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

class Main{
	public static void main(String args[]){
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = new String();
		try{
			while((str = br.readLine()) != null){
				int array[][] = decode(str);

				String result = new String();
				for(int i = 0; i < array.length; i++){
					//System.out.print(array[i][0]);
					//System.out.print(array[i][1]);
					result += outputString(array[i][0], array[i][1]);
				}
				System.out.println(result);
			}
		}catch(Exception e){
			System.err.println(e);
		}
	}

	static int[][] decode(String input){
		// とりあえず中間テキストに置き換え
		ArrayList<String> middleArray1 = new ArrayList<String>();
		ArrayList<String> middleArray2 = new ArrayList<String>();
		char now_char = input.charAt(0);
		int char_count = 1;

		for(int i = 1; i < input.length(); i++){
			//System.out.println("charat");
			char pointing_char = input.charAt(i);
			if(now_char == pointing_char){
				char_count++;
			}else{
				//middle_str += now_char;
				middleArray1.add("" + now_char);
				// middle_str += char_count;
				middleArray2.add("" + char_count);
				now_char = pointing_char;
				char_count = 1;
			}
		}
		//middle_str += now_char;
		middleArray1.add("" + now_char);
		// middle_str += char_count;
		middleArray2.add("" + now_char);

		// 中間テキストから配列へ
		int result[][] = new int[middleArray1.size()][2];
		//System.out.println("middle  " + );
		//System.out.println("middle length " + middle_str.length());
		for(int i = 0; i < middleArray1.size(); i++){
			//System.out.println("middle to array " + i);
			result[i][0] = Integer.parseInt(middleArray1.get(i));
			result[i][1] = Integer.parseInt(middleArray2.get(i));
		}
		return result;
	}

	static String outputString(int m, int n){
		String result_str = "";
		switch(m){
		case 1:{
			String strs[] = {"'", ",", ".", "!", "?"};
			result_str = strs[(n-1) % strs.length];
			break;
		}
		case 2:{
			String strs[] = {"a", "b", "c", "A", "B", "C"};
			result_str = strs[(n-1) % strs.length];
			break;
		}
		case 3:{
			String strs[] = {"d", "e", "f", "D", "E", "f"};
			result_str = strs[(n-1) % strs.length];
			break;
		}
		case 4:{
			String strs[] = {"g", "h", "i", "G", "H", "I"};
			result_str = strs[(n-1) % strs.length];
			break;
		}
		case 5:{
			String strs[] = {"j", "k", "l", "J", "K", "L"};
			result_str = strs[(n-1) % strs.length];
			break;
		}
		case 6:{
			String strs[] = {"m", "n", "o", "M", "N", "O"};
			result_str = strs[(n-1) % strs.length];
			break;
		}
		case 7:{
			String strs[] = {"p", "q", "r", "s", "P", "Q", "R", "S"};
			result_str = strs[(n-1) % strs.length];
			break;
		}
		case 8:{
			String strs[] = {"t", "u", "v", "T", "U", "V"};
			result_str = strs[(n-1) % strs.length];
			break;
		}
		case 9:{
			String strs[] = {"w", "x", "y", "z", "W", "X", "Y", "Z"};
			result_str = strs[(n-1) % strs.length];
			break;
		}
		case 0:{
			for(int i = 1; i < n; i++){
				result_str += " ";
			}
		}
		}
		return result_str;
	}
}