gpt4 book ai didi

java - 如果用户未输入二进制值,我如何抛出异常

转载 作者:行者123 更新时间:2023-11-30 06:14:23 25 4
gpt4 key购买 nike

我想将输入作为来自用户的二进制值。如果他不输入二进制(例如:3214),那么我要给出WrongFormatException。但是我无法处理 try-throw-catch 的事情。这是我的 Controller 类。

我稍微努力一下,改成这样。有什么有趣的部分我应该改变吗?顺便说一句,它正在工作。谢谢帮助

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

}
}

最佳答案

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

关于java - 如果用户未输入二进制值,我如何抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30408327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com