Java NumberFormatException方法
大家好,我想请人帮我。我想在同一个类中编写一个方法,使用Java NumberFormatException方法,java,swing,awt,numberformatexception,Java,Swing,Awt,Numberformatexception,大家好,我想请人帮我。我想在同一个类中编写一个方法,使用NumberFormatException,它获取JTextField的值,并检查它是否为number并打印。另外,如何在actionPerformed方法中实现此代码 这是主要的方法 public class main { public static void main(String args[]){ JavaApplication19 is = new JavaApplication19("title");
NumberFormatException
,它获取JTextField
的值,并检查它是否为number并打印。另外,如何在actionPerformed
方法中实现此代码
这是主要的方法
public class main {
public static void main(String args[]){
JavaApplication19 is = new JavaApplication19("title");
is.setVisible(true);
}
}
这是GUI类:
public class JavaApplication19 extends JFrame{
private final JButton button;
private final JTextField text;
private final JLabel lable;
/**
* @param args the command line arguments
*/
public JavaApplication19(String title){
setSize(500, 200);
setTitle(title);
setDefaultCloseOperation(JavaApplication19.EXIT_ON_CLOSE);
button = new JButton("enter only number or i will kill you");
text = new JTextField();
lable = new JLabel("numbers only");
JPanel rbPanel = new JPanel(new GridLayout(2,2));
rbPanel.add(button);
rbPanel.add(text);
rbPanel.add(lable);
Container pane = getContentPane();
pane.add(rbPanel);
button.addActionListener(new ButtonWatcher());
private class ButtonWatcher implements ActionListener{
public void actionPerformed(ActionEvent a){
Object buttonPressed=a.getSource();
if(buttonPressed.equals(button))
{
}
}
}
}
//try something
应该是获取输入文本和解析的代码(例如Integer.parseInt(textField.getText())
)。如果由于未输入数字以外的内容而导致解析不起作用,它将抛出NumberFormatException
查看是否需要有关如何使用异常的更多信息
编辑:方法
像这样简单的事情就行了
public int parseInput(String input) throws NumberFormatException {
return Integer.parseInt(input);
}
或者类似的东西,如果你想捕捉异常
public static int parseInput(String input) {
int number = 0;
try {
number = Integer.parseInt(input);
} catch (NumberFormatException ex) {
someLabel.setText("Must be a number");
return -1; // return 0
}
}
那么在你的行动中,你可以做这样的事情
if(buttonPressed.equals(button))
{
int n;
if (parseInput(textField.getText()) != -1){
n = parseInput(textField.getText());
// do something with n
}
}
编辑:布尔方法
用法
编辑:或捕获异常
public static int parseInput(String input) {
int number = 0;
try {
number = Integer.parseInt(input);
} catch (NumberFormatException ex) {
someLabel.setText("Must be a number");
return -1; // return 0
}
}
你知道如何使用
try/catch
块吗?try{something}catch(NumberFormatException ex){Do something}
是的,它最可能是try/catch和it-most-be方法我认为它最可能是采用一个参数的方法,该参数是文本字段的值,如果为真则返回true,如果为假则返回false,如果为假则返回false。你能告诉我你将如何在代码中实现它吗?另外,如果为真,则返回true,如果为假则返回false,我使用布尔方法编辑。但是,如果这是一个要求,它就不会使用NumberFormatException
。只要把这个方法放在你的类中,你就可以使用它了。我刚刚用NumberFormatException
编辑了一个方法。你有很多选择。选择一个。该方法应放在类中,构造函数之外。
public boolean isNumber(String input){
for (char c : input.toCharArray()){
if (!Character.isDigit(c))
return false;
}
return true;
}
if(buttonPressed.equals(button))
{
if (isNumber(textField.getText()){
// do something
}
}
public boolean isNumber(String input){
try {
Integer.parseInt(input);
return true;
} catch (NumberFormatException ex){
return false;
}
}