[英]The method parseInt(String) in the type Integer is not applicable for the arguments (boolean)
I am getting the error The method parseInt(String) in the type Integer is not applicable for the arguments (boolean)我收到错误Integer 类型中的方法 parseInt(String) 不适用于参数(布尔值)
Here is the code这是代码
} else if (Integer.parseInt(answerField.getText() == null)) {
JOptionPane.showMessageDialog(null, "You did not enter an answer!");
}
I have also tried this but it doesn't work:我也试过这个,但它不起作用:
(Integer.parseInt(answerField.getText().equals("")))
& &
(Integer.parseInt(answerField.getText().length()) == 0)
I just want to check to see if nothing has been entered and if so display a JOptionPane.我只想检查是否没有输入任何内容,如果是,则显示 JOptionPane。
Edit: The var answerField is a JTextField, where the user inputs an answer of a mathematical question.编辑: var answerField 是一个 JTextField,用户在其中输入数学问题的答案。 So the ActionListener then determines if the answer is correct, hence the parseInt (because of it being a mathematical operation)
所以 ActionListener 然后确定答案是否正确,因此 parseInt (因为它是一个数学运算)
do {
checkButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Integer.parseInt(answerField.getText()) == correctAnswer) {
count++;
JOptionPane.showMessageDialog(null, "Correct");
} else if (Integer.parseInt(answerField.getText().length()) == 0) {
JOptionPane.showMessageDialog(null, "You did not enter an answer!");
} else {
count++;
JOptionPane.showMessageDialog(null, "Incorrect!");
System.exit(0);
}
}
});
} while (count < 11);
Lets analise the code you have让我们分析您拥有的代码
(Integer.parseInt(answerField.getText() == null))
the parameter参数
answerField.getText() == null
return a boolean and the method Integer.parseInt(bool)
is not defined in the Integer class.返回一个布尔值,并且
Integer.parseInt(bool)
方法未在 Integer 类中定义。
It looks like you want to do:看起来你想做:
else if (someConditionHere) {
Integer.parseInt(answerField.getText());
JOptionPane.showMessageDialog(null, "You did not enter an answer!");
}
all of the values inside of your parse int method return a boolean becuase they are performing a comparison between two values that result in either true or false. parse int 方法中的所有值都返回一个布尔值,因为它们正在执行两个值之间的比较,结果要么是真要么是假。 You should use Boolean.parseBoolean() to return either "true" or "false" strings
您应该使用 Boolean.parseBoolean() 返回“true”或“false”字符串
Integer.parseInt(answerField.getText() == null)
is evaluated as Integer.parseInt("someTextext" == null)
then Integer.parseInt(answerField.getText() == null)
被评估为Integer.parseInt("someTextext" == null)
然后
Integer.parseInt(true/false)
Try to parse a boolean as an Integer will result as an error尝试将布尔值解析为整数将导致错误
To check is your field is null, only do if(answerField.getText() == null)
要检查您的字段是否为空,请仅执行
if(answerField.getText() == null)
then if is not null you can try to parse the field value to an Integer the way you did.那么如果不为空,您可以尝试按照您的方式将字段值解析为整数。
I am not sure why you want to call Integer.parseInt, because you already have a boolean expression in your condition:我不确定您为什么要调用 Integer.parseInt,因为您的条件中已经有一个布尔表达式:
} else if (answerField.getText() == null) {
JOptionPane.showMessageDialog(null, "You did not enter an answer!");
}
Note that if conditions in Java can only accept boolean expressions, and you couldn't pass in an integer or whatever else you'd think of as truthy or falsy (unlike C or Javascript, for example).请注意,如果 Java 中的条件只能接受布尔表达式,并且您不能传入整数或任何其他您认为为真或假的东西(例如,与 C 或 Javascript 不同)。
If all you want to do is check to see if there's some string contained in answerField
, then all you need to do is check to see if it's not null or empty.如果您要做的只是检查
answerField
中是否包含一些字符串,那么您需要做的就是检查它是否不为空或为空。
if(!("".equals(answerField.getText()) || null == answerField.getText()) {
// other, non-integer-handling code here
}
You do not want to handle parsing the integer if there's an empty string or if it's null
, since that could result in a NullPointerException
.如果有一个空字符串或者它是
null
,您不想处理解析整数,因为这可能会导致NullPointerException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.