如果用户没有输入二进制值,我怎么能抛出异常

时间:2015-05-23 02:36:51

标签: java exception binary try-catch throw

我想将输入作为用户的二进制值。如果他没有输入二进制文件(例如:3214),那么我想给出WrongFormatException。但我无法管理try-throw-catch事情。这是我的控制器类。

我工作了一点,然后把它换成了它。我应该改变哪些有趣的部分?它的工作顺便说一句。谢谢你的帮助

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Controller {

private View theView;
private Model theModel;

public Controller( View theView, Model theModel ){

    this.theView = theView;
    this.theModel = theModel;

    theView.addListener( new MyActionListener());   
}

public class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent event) {

        int inputBinary = theView.getInputBinary();

        try {
            if ( checkFormat(inputBinary) )
            {
                int outputDecimal = theModel.convertToDecimal(Integer.toString(inputBinary));
                theView.setOutputDecimal(outputDecimal);
            }                   
            else
            {
                throw new WrongFormatException( "Wrong format");
            }

        } catch ( WrongFormatException e ) {

            theView.setOutputDecimal(0);
            theView.displayErrorMessage( "Wrong Format");
        }

    }
}

public boolean checkFormat ( int input )
{
    // check if it is not in true format
    String stringInput = Integer.toString(input);           
    for ( int i = 0; i < stringInput.length(); i++ )
    {
        if ( stringInput.charAt(i) == '0' || stringInput.charAt(i) == '1' )
        {

        }
        else
        {
            return false;
        }
    }
    return true;
}

// my own exception class
public class WrongFormatException extends Exception
{

    // CONSTRUCTOR

    public WrongFormatException( String message )
    {
        super( message );
    }

}   
}

这里的第一个代码,但忽略这一部分。

 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;


public class Controller {

private View theView;
private Model theModel;

public Controller( View theView, Model theModel ){

    this.theView = theView;
    this.theModel = theModel;

    theView.addListener( new MyActionListener());   
}

public class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent event) {

        // we got the input
        int inputBinary = theView.getInputBinary();

        // create an exception
        WrongFormatException problem = new WrongFormatException( "Input value is not binary");

        // base value, we assme the input is in true format
        boolean isTrueFormat = true;

        // check if it is not in true format
        String stringBinary = Integer.toString(inputBinary);            
        for ( int i = 0; i < stringBinary.length(); i++ )
        {
            if ( stringBinary.charAt(i) == '0' || stringBinary.charAt(i) == '1' )
            {

            }
            else
            {
                isTrueFormat = false;
                i = stringBinary.length();
            }
        }

        // we do the convertion work here
        int outputDecimal = theModel.convertToDecimal(Integer.toString(inputBinary));

        // and we set output textfield
        theView.setOutputDecimal(outputDecimal);


    }
}

// my own exception class
public class WrongFormatException extends Exception
{

    // CONSTRUCTOR

    public WrongFormatException( String message )
    {
        super( message );
    }

}   
}

3 个答案:

答案 0 :(得分:2)

如果扩展java.lang.RuntimeException而不是java.lang.Exception,则可以从else块抛出WrongFormatException而不声明throws子句

答案 1 :(得分:2)

您可以使用Integer.parseInt(String s, int radix)验证输入是否为二进制。如果输入格式不正确,parseInt将抛出NumberFormatException异常,这使得解析和检查是否有效非常容易。

Scanner in = new Scanner(System.in);

while (true) {
    System.out.println("Enter a binary value: ");
    String input = in.nextLine();

    int value = 0;
    try {
        value = Integer.parseInt(input, 2);
    } catch (NumberFormatException nfex) {
        throw new WrongFormatException("Input value is not binary");// incorrect format
    }

    System.out.println("You entered \"" + value + "\" in base 10");
}

有一件事要注意,parseInt上的文档说:

  

...除了第一个字符可能是ASCII减号' - '('\ u002D')表示负值或ASCII加号'+'('\ u002B')表示正值...

所以你可能需要检查一下......

编辑:从评论中看到我错过了重点。你的新代码非常好。

答案 2 :(得分:1)

用户输入一个String,因此请将其作为字符串进行测试,如下所示:

if (!input.matches("[01]+"))
    // it's not a binary, so throw exception etc