Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JTextfields中检索整数并将其应用于求和_Java_Swing_Parsing - Fatal编程技术网

Java 从JTextfields中检索整数并将其应用于求和

Java 从JTextfields中检索整数并将其应用于求和,java,swing,parsing,Java,Swing,Parsing,我有一些JTextfields,它们根据某些事件而变化,例如,如果勾选了一个框,则文本字段的内容将从0变为35 我试图将文本字段的值添加到一起,但似乎无法做到这一点 int f1 = 35; int f2 = 18; apple.setText("" + f1); pear.setText("" + f2); 这是我到目前为止所拥有的 int result = Integer.parseInt(apple.getText() + Integer.parseInt(pear.getText()

我有一些
JTextfield
s,它们根据某些事件而变化,例如,如果勾选了一个框,则文本字段的内容将从
0
变为
35

我试图将文本字段的值添加到一起,但似乎无法做到这一点

int f1 = 35;
int f2 = 18;

apple.setText("" + f1);
pear.setText("" + f2);
这是我到目前为止所拥有的

int result = Integer.parseInt(apple.getText() + Integer.parseInt(pear.getText());
                total.setText("" +  result);

当我需要将
f1
f2
添加到一起时,结果为我提供了
3518
,您所做的是先连接两个字符串,然后解析生成的字符串,而不是您想要的。您要做的是在将文本添加到一起之前分别解析它们

try {
    int appleInt = Integer.parseInt(apple.getText());
    int pearInt = Integer.parseInt(pear.getText());

    int result = appleInt + pearInt;

    // do something with result

} catch (NumberFormatException nfe) {
    // warn user that text is wrong
    // clear text fields
}

您要做的是首先连接两个字符串,然后解析结果字符串——这不是您想要的。您要做的是在将文本添加到一起之前分别解析它们

try {
    int appleInt = Integer.parseInt(apple.getText());
    int pearInt = Integer.parseInt(pear.getText());

    int result = appleInt + pearInt;

    // do something with result

} catch (NumberFormatException nfe) {
    // warn user that text is wrong
    // clear text fields
}
你应该这样做

int result = Integer.parseInt(apple.getText()) + Integer.parseInt(pear.getText());
您的代码首先将pear文本转换为整数,并将其相加为apple文本字符串(int+String=String),然后将总数转换为整数,但在将int与字符串相加时,仅将两个值串联在一起;相反,您需要将两个字符串转换为整数,然后求和

不要忘记检查字符串的值是否为整数,否则程序将抛出NumberFormatException。

您应该这样做

int result = Integer.parseInt(apple.getText()) + Integer.parseInt(pear.getText());
您的代码首先将pear文本转换为整数,并将其相加为apple文本字符串(int+String=String),然后将总数转换为整数,但在将int与字符串相加时,仅将两个值串联在一起;相反,您需要将两个字符串转换为整数,然后求和

不要忘记检查字符串的值是否为整数,否则程序将抛出NumberFormatException。

试试这个

int result = Integer.parseInt(apple.getText()) + Integer.parseInt(pear.getText());
而不是:

int result = Integer.parseInt(apple.getText() + Integer.parseInt(pear.getText());
在这里,我实际上分别解析了
apple.getText()
pear.getText()
,然后添加整数以得到结果。在您上传的代码中,您基本上是将
apple.getText()
与整数解析值
pear.getText()

连接在一起

int result = Integer.parseInt(apple.getText()) + Integer.parseInt(pear.getText());
而不是:

int result = Integer.parseInt(apple.getText() + Integer.parseInt(pear.getText());

在这里,我实际上分别解析了
apple.getText()
pear.getText()
,然后添加整数以得到结果。在您上传的代码中,您基本上是将
apple.getText()
与整数解析值
pear.getText()

串联在一起,您希望在添加之前对它们进行单独解析。您对括号做了一些错误。您希望单独解析它们,在添加之前,您在括号中犯了一些错误。