0

再会。如果出现错误,我下面的程序应该给用户另一个机会继续,如果没有更多错误,即输入有效,则停止进程。

import java.util.*;
import javax.swing.*;

public class Format{

    public static boolean formatter(String phoneNumber){
        boolean thereIsAnError = true;
        try {
        String telephone = "";

        if (phoneNumber.length() != 10)
            throw new InputMismatchException();
        long number = Long.parseLong(phoneNumber);
        if (number < 0)
            throw new InputMismatchException();

        while (number > 0) {
            telephone = telephone + " " + number%10;
            number = number/10;
        }

        StringTokenizer numTokenizer = null;
        numTokenizer = new StringTokenizer(telephone);
        int numOfTokens = numTokenizer.countTokens();
        StringBuilder formatted = new StringBuilder();
        String[] numberArray = new String[10];

        for (int i=0; i<numOfTokens; i++) {
            numberArray[i] = numTokenizer.nextToken();
        }

        formatted.append("(");
        if (numberArray[9] == null)
            numberArray[9] = "0";
        formatted.append(numberArray[9]);
        if (numberArray[8] == null)
            numberArray[8] = "0";
        formatted.append(numberArray[8]);
        if (numberArray[7] == null)
            numberArray[7] = "0";
        formatted.append(numberArray[7]);
        if (numberArray[6] == null)
            numberArray[6] = "0";
        formatted.append(")");
        formatted.append(" ");
        formatted.append(numberArray[6]);
        if (numberArray[5] == null)
            numberArray[5] = "0";
        formatted.append(numberArray[5]);
        if (numberArray[4] == null)
            numberArray[4] = "0";
        formatted.append(numberArray[4]);
        if (numberArray[3] == null)
            numberArray[3] = "0";
        formatted.append("-");
        formatted.append(numberArray[3]);
        if (numberArray[2] == null)
            numberArray[2] = "0";
        formatted.append(numberArray[2]);
        if (numberArray[1] == null)
            numberArray[1] = "0";
        formatted.append(numberArray[1]);
        if (numberArray[0] == null)
            numberArray[0] = "0";
        formatted.append(numberArray[0]);

        formatted.toString();
        JOptionPane.showMessageDialog(null, formatted);
        }
        catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, "Please enter only digits. No other characters.", "This is fun." , JOptionPane.ERROR_MESSAGE);
        }
        catch (InputMismatchException e) {
            JOptionPane.showMessageDialog(null, "Please enter 10 positive digits.", "This is fun." , JOptionPane.ERROR_MESSAGE);
        }
        return thereIsAnError;
    }

    public static void main(String[] args) {

        String inputNumber = "This program receives a 10-digit phone number as input\nand produces a phone number formatted as (xxx) xxx-xxxx.\n\nInput a 10-digit phone number (just numbers, nothing else):";
        boolean thereIsAnError = true;

        while (thereIsAnError) {
        try {
            String phoneNumber = JOptionPane.showInputDialog(inputNumber);
            phoneNumber = phoneNumber.trim();
            thereIsAnError = formatter(phoneNumber);

        }

        catch (NullPointerException e){
            JOptionPane.showMessageDialog(null, "Thank you. Goodbye.");
            System.exit(0);
        }

        catch (Exception e) { 
            JOptionPane.showMessageDialog(null, "There is something wrong. Shutting down.");
        }
        finally {
            JOptionPane.showMessageDialog(null, "All exceptions avoided or handled!");
        }
        }

    }

}


我想我没有放置正确的 catch 块。请帮我找出问题所在。我是 Java 新手,我还在学习。谢谢!

4

2 回答 2

0

当提供正确的输入时,您需要设置thereIsAnError为 false 以便循环停止。

大概之后...

 formatted.toString();
 JOptionPane.showMessageDialog(null, formatted);
 thereIsAnError = false;  // <--- No error should be reflected and returned.
 }
于 2012-08-05T11:43:34.583 回答
0

thereIsAnError永远不会分配给不匹配的场景。另外,我建议您看看使用正则表达式进行此类工作。

于 2012-08-05T11:46:38.190 回答