简体   繁体   English

Integer 类型中的方法 parseInt(String) 不适用于参数(布尔值)

[英]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.

相关问题 Integer.parseInt返回错误:Integer类型的方法parseInt(String)不适用于参数(R.string) - Integer.parseInt Returns an error: The method parseInt(String) in the type Integer is not applicable for the arguments (R.string) integer 类型中的方法 parseInt() 不适用于 Action Listener (Java) 中的 arguments (JTextField) - The method parseInt() in the type integer is not applicable for the arguments (JTextField) in Action Listener (Java) 方法put(String,ArrayList <Integer> ),类型为TreeMap <String,ArrayList<Integer> &gt;不适用于参数(字符串,布尔值) - The method put(String, ArrayList<Integer>) in the type TreeMap<String,ArrayList<Integer>> is not applicable for the arguments (String, boolean) 错误:ArrayList 类型中的方法 add(Integer)<integer> 不适用于 arguments(字符串)</integer> - ERROR: The method add(Integer) in the type ArrayList<Integer> is not applicable for the arguments (String) Intent类型的方法putExtra(String,boolean)不适用于参数(String,CarouselDataItem) - The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, CarouselDataItem) Integer类型的方法valueOf(String)不适用于参数(int) - The method valueOf(String) in the type Integer is not applicable for the arguments (int) 如何修复[ContentValues类型的put(String,Boolean)方法不适用于自变量(Boolean,Boolean)] - How to fix [The method put(String, Boolean) in the type ContentValues is not applicable for the arguments (Boolean, Boolean)] 如何修复错误:Map 类型中的 put(Integer, Integer) 方法<integer,integer>不适用于 arguments (int, String)</integer,integer> - How to fix error: The method put(Integer, Integer) in the type Map<Integer,Integer> is not applicable for the arguments (int, String) 错误 java 方法组合Sum(int[], int, List<integer> ) 类型中的解决方案不适用于 arguments (int[], int, boolean)</integer> - error java The method combinationSum(int[], int, List<Integer>) in the type Solution is not applicable for the arguments (int[], int, boolean) 将textView转换为Boolean以避免:Intent类型中的方法putExtra(String,boolean)不适用于参数 - Converting textView to Boolean to avoid: The method putExtra(String, boolean) in the type Intent is not applicable for the arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM