Java 如何检查字符串是否为int
好了,伙计们,我基本上做了一个GUI Java程序,一个基本的程序。只需将文本字段中的数字相加/subs/divides或相乘,这没什么大不了的,因为我刚刚开始学习Java。它可以工作,但也有一些错误,比如当你执行它时,如果你点击其中一个单选按钮而没有在文本字段中输入数字,那么程序将无法工作。当用户单击单选按钮时,如何检查用户是否输入了整数? 代码如下:Java 如何检查字符串是否为int,java,user-interface,Java,User Interface,好了,伙计们,我基本上做了一个GUI Java程序,一个基本的程序。只需将文本字段中的数字相加/subs/divides或相乘,这没什么大不了的,因为我刚刚开始学习Java。它可以工作,但也有一些错误,比如当你执行它时,如果你点击其中一个单选按钮而没有在文本字段中输入数字,那么程序将无法工作。当用户单击单选按钮时,如何检查用户是否输入了整数? 代码如下: import java.awt.event.*; import java.text.NumberFormat; import javax.sw
import java.awt.event.*;
import java.text.NumberFormat;
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame{
Button button1;
TextField num1;
TextField num2;
JRadioButton add,sub,mul,div;
boolean isnumber = false;
int x, y, sum;
public static void main(String[] args){
new GUI();
}
public GUI(){
thehandler handle = new thehandler();
JPanel panel = new JPanel();
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setSize(800, 70);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Calc");
this.add(panel);
button1 = new Button("Calculate");
button1.addActionListener(handle);
panel.add(button1);
num1 = new TextField("Enter a number here");
num1.addActionListener(handle);
num2 = new TextField("Enter a number here");
num2.addActionListener(handle);
panel.add(num1);
panel.add(num2);
add = new JRadioButton("Add");
add.addActionListener(handle);
sub = new JRadioButton("Subtract");
sub.addActionListener(handle);
mul = new JRadioButton("Multiply");
mul.addActionListener(handle);
div = new JRadioButton("Divide");
div.addActionListener(handle);
ButtonGroup operation = new ButtonGroup();
operation.add(add);
operation.add(sub);
operation.add(div);
operation.add(mul);
panel.add(add);
panel.add(sub);
panel.add(mul);
panel.add(div);
}
private class thehandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == add){
sum = x + y;
x = Integer.parseInt(num1.getText());
y = Integer.parseInt(num2.getText());
}
if(e.getSource() == sub){
sum = x - y;
x = Integer.parseInt(num1.getText());
y = Integer.parseInt(num2.getText());
}
if(e.getSource() == mul){
sum = x * y;
x = Integer.parseInt(num1.getText());
y = Integer.parseInt(num2.getText());
}
if(e.getSource() == div){
sum = x/y;
x = Integer.parseInt(num1.getText());
y = Integer.parseInt(num2.getText());
}
if(e.getSource() == button1){
JOptionPane.showMessageDialog(null, "The sum of the desired calculation is... " + sum);
}
}
}
}
使用
tocharray()
然后循环检查每个字符isDigit()
编辑*
正如其他人所说,捕获异常是处理此问题的更好方法使用
tocharray()
然后循环检查每个字符isDigit()
编辑*
正如其他人所说,捕获异常是处理此问题的更好方法,只需执行以下操作:
String text = num1.getText();
try {
Integer x = Integer.parseInt(text);
} catch (NumberFormatException) {
System.out.println(text + " cannot be converted to integer");
}
如果无法将字符串解析为整数
,将抛出NumberFormatException
。只需执行以下操作:
String text = num1.getText();
try {
Integer x = Integer.parseInt(text);
} catch (NumberFormatException) {
System.out.println(text + " cannot be converted to integer");
}
如果无法将字符串解析为
整数
,则将抛出NumberFormatException
。当Integer.parseInt(…)
被赋予无法解析的内容时,将抛出NumberFormatException
。您应该将parseInt(…)
调用包装在try/catch块中,并适当地处理这种情况:
try {
x = Integer.parseInt(num1.getText());
} catch (NumberFormatException nfe) {
// do something about broken data
}
使用单独的方法进行所有解析和处理,以减少代码重复量,这将非常方便。当
Integer.parseInt(…)
被赋予它无法解析的内容时,它会抛出一个NumberFormatException
。您应该将parseInt(…)
调用包装在try/catch块中,并适当地处理这种情况:
try {
x = Integer.parseInt(num1.getText());
} catch (NumberFormatException nfe) {
// do something about broken data
}
使用单独的方法进行所有解析和处理将非常方便,以减少代码重复量您可以使用如下函数:
boolean isInt(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
boolean-isInt(字符串str){
对于(int i=0;i
您可以使用如下功能:
boolean isInt(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
boolean-isInt(字符串str){
对于(int i=0;i
您应该检查文本字段是否为空,并且应该在整数解析中添加一些错误处理,如:
try {
x = Integer.parseInt(num1.getText())>
catch (NumberFormatException ex) {
//error handling
}
如果TextField为空,则应进行检查,并向整数解析添加一些错误处理,如:
try {
x = Integer.parseInt(num1.getText())>
catch (NumberFormatException ex) {
//error handling
}
这取决于整数的含义,但如果您只想检查字符串是否只包含数字,并且可以选择在开始时使用类似正则表达式的
yourString.matches("-?\\d+");
请注意,这不会检查数字的范围。这取决于整数的含义,但如果您只想检查字符串是否只包含数字,并且可以选择在开始时使用类似regex的
yourString.matches("-?\\d+");
请注意,这不会检查数字的范围。在其周围放置一个try catch并捕获
NumberFormatException
。在其周围放置一个try catch并捕获NumberFormatException
。请参见@Kuchi well,regex引擎也不是很轻,但它肯定比捕获异常更容易使用。@Kuchi well,regex引擎也不是很轻,但它肯定比捕获异常更容易使用。NumberFormatException需要一个标识符,但除此之外,这段代码对我来说工作得很好!NumberFormatException需要一个标识符,但除此之外,这段代码对我来说运行良好!