如果没有更多例外,我怎么能停止输入?

时间:2012-08-05 11:38:11

标签: java exception exception-handling try-catch

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

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!");
        }
        }

    }

}


我想我没有放置正确的挡块。请帮我弄清楚什么是错的。我是Java的新手,我还在学习。谢谢!

2 个答案:

答案 0 :(得分:0)

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

可能是......

 formatted.toString();
 JOptionPane.showMessageDialog(null, formatted);
 thereIsAnError = false;  // <--- No error should be reflected and returned.
 }

答案 1 :(得分:0)

永远不会为非匹配方案分配

thereIsAnError。另外,我建议您查看使用Regular Expressions进行此类工作。