Java 如果用户未输入二进制值,如何引发异常
我想将输入作为用户的二进制值。如果他没有输入二进制(例如:3214),那么我想给出一个错误的异常。但我没法试着扔接球。这是我的控制器类 我做了一点工作,并把它改成了。有什么有趣的地方需要我改变吗?顺便说一句,它很有用。谢谢你的帮助Java 如果用户未输入二进制值,如何引发异常,java,exception,binary,try-catch,throw,Java,Exception,Binary,Try Catch,Throw,我想将输入作为用户的二进制值。如果他没有输入二进制(例如:3214),那么我想给出一个错误的异常。但我没法试着扔接球。这是我的控制器类 我做了一点工作,并把它改成了。有什么有趣的地方需要我改变吗?顺便说一句,它很有用。谢谢你的帮助 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Controller { private View theView; private Mo
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 );
}
}
}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
公共类控制器{
私人观点;
私有模型;
公共控制器(查看视图、建模模型){
this.theView=theView;
this.theModel=模型;
addListener(新的MyActionListener());
}
公共类MyActionListener实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件){
int-inputBinary=theView.getInputBinary();
试一试{
if(检查格式(输入二进制))
{
int outputDecimal=model.convertToDecimal(Integer.toString(inputBinary));
setOutputDecimal(outputDecimal);
}
其他的
{
抛出新的错误格式异常(“错误格式”);
}
}捕获(错误格式化异常){
setOutputDecimal(0);
theView.displayErrorMessage(“格式错误”);
}
}
}
公共布尔校验格式(整数输入)
{
//检查它是否不是真实格式
字符串stringInput=Integer.toString(输入);
对于(int i=0;i
首先在此处输入代码,但忽略此部分
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 );
}
}
}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
公共类控制器{
私人观点;
私有模型;
公共控制器(查看视图、建模模型){
this.theView=theView;
this.theModel=模型;
addListener(新的MyActionListener());
}
公共类MyActionListener实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件){
//我们得到了信息
int-inputBinary=theView.getInputBinary();
//创建一个异常
ErrorFormatException问题=新的ErrorFormatException(“输入值不是二进制”);
//基本值,我们假定输入为真格式
布尔isTrueFormat=true;
//检查它是否不是真实格式
字符串stringBinary=Integer.toString(inputBinary);
对于(int i=0;i
如果扩展java.lang.RuntimeException而不是java.lang.Exception,则可以从else块抛出ErrorFormatException,而无需声明throws子句您可以使用它来验证输入是否为二进制<如果输入的格式不正确,code>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')表示正值外
所以你可能需要检查一下
编辑:从评论中看到我没有抓住要点。你的新代码非常好 用户输入一个字符串,因此请将其作为字符串进行测试,如下所示:
if (!input.matches("[01]+"))
// it's not a binary, so throw exception etc
哇。太棒了。但是,我想学习如何为我的考试抛出异常:(我刚刚选择了binary示例在这种情况下,这可能会有所帮助:旁注:在Java中,异常的堆栈信息是在构造时存储的,而不是在抛出时存储的(如.NET中)因此,考虑调用<代码> new DealFrimeExtabor >代码>更接近于抛出异常的点。在大多数情况下,异常代码看起来像:<代码>抛出新的< /代码>…如果抛出一个<代码>错误格式异常>代码>,(如果在<代码> MyActRistListor执行时< /代码>)有什么意义?是的,我也对此提出了质疑。但我只是想学习抛出异常:DI放弃了checkFormat方法。谢谢