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

Java,运行时异常,又看不了报错信息。

Posted by yangdaren at 2019-11-26 14:33:56 on Problem 1002
package oj;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int rows, i, j, phoneNumberCharLength, char2Number;
        String phoneNumberInput;
        StringBuilder phoneNumberFormat = new StringBuilder();
        char[] phoneNumberCharArray;
        Map<String, Integer> map = new HashMap<String, Integer>(16);
        Scanner scanner = new Scanner(System.in);
        rows = scanner.nextInt();
        scanner.nextLine();
        for (i = 0; i < rows; i++) {
            phoneNumberFormat.delete(0, phoneNumberFormat.length());
            phoneNumberInput = scanner.nextLine();
            phoneNumberCharArray = phoneNumberInput.toCharArray();
            phoneNumberCharLength = phoneNumberCharArray.length;
            for (j = 0; j < phoneNumberCharLength; j++) {
                char2Number = Character.getNumericValue(phoneNumberCharArray[j]);
                if (char2Number != -1) {
                    if (char2Number <= 9) {
                        phoneNumberFormat.append(phoneNumberCharArray[j]);
                        continue;
                    }
                }
                if (phoneNumberCharArray[j] == '-') {
                    continue;
                }
                switch (phoneNumberCharArray[j]) {
                    case 'A':
                    case 'B':
                    case 'C':
                        phoneNumberFormat.append(2);
                        break;
                    case 'D':
                    case 'E':
                    case 'F':
                        phoneNumberFormat.append(3);
                        break;
                    case 'G':
                    case 'H':
                    case 'I':
                        phoneNumberFormat.append(4);
                        break;
                    case 'J':
                    case 'K':
                    case 'L':
                        phoneNumberFormat.append(5);
                        break;
                    case 'M':
                    case 'N':
                    case 'O':
                        phoneNumberFormat.append(6);
                        break;
                    case 'P':
                    case 'R':
                    case 'S':
                        phoneNumberFormat.append(7);
                        break;
                    case 'T':
                    case 'U':
                    case 'V':
                        phoneNumberFormat.append(8);
                        break;
                    case 'W':
                    case 'X':
                    case 'Y':
                        phoneNumberFormat.append(9);
                        break;
                    default:
                }
            }
            //在第三位后插入小横杠"-"
            if (phoneNumberFormat.length() > 3) {
                phoneNumberFormat.insert(3, "-");
            }
            //对map进行判断,计数
            if (map.containsKey(phoneNumberFormat.toString())) {
                Integer count = map.get(phoneNumberFormat.toString());
                count++;
                map.put(phoneNumberFormat.toString(), count);
            } else {
                map.put(phoneNumberFormat.toString(), 1);
            }
        }
        //如果keySet都为1
        Set<String> keySet = map.keySet();
        Object[] keyArray = keySet.toArray();
        int length = keyArray.length;
        for (i = 0; i < length; i++) {
            if (map.get(keyArray[i]) == 1) {
                map.remove(keyArray[i]);
            }
        }
        //更新set值
        keySet = map.keySet();
        keyArray = keySet.toArray();
        length = keyArray.length;
        if (map.size() > 0) {
            Arrays.sort(keyArray);
            for (i = 0; i < length; i++) {
                System.out.println(keyArray[i] + " " + map.get(keyArray[i]));
            }
        } else {
            System.out.println("No duplicates.");
        }

    }
}

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