Search code examples
javaswingintegerjtextfield

Java Incompatiable Types; JTextField and Integer


For complete code, go to http://ideone.com/7XHxSm

I'm trying to use a JTextField then pull the string value then parse the value into an integer. Once parsed, I need to multiply them together. Later on, I will add them all up, add & multiply to get an extra amount, then divide to get a number.

Once you see my code, you'll understand. I know it's a bit cryptic but the code will explain.

I don't know what is going on because I am pulling the value from the JTextField then converting it to an integer but Java refuses to convert it.

widthWall2 = Integer.parseInteger(wall2Width.getText());
heighthWall2 = Integer.parseInteger(wall2Heighth.getText());
//wall2Area = widthWall2*heighthWall2;
wall2Area = Integer.valueOf(wall2Width.getText()) * integer.valueOf(wall2Heighth.getText());

Solution

  • JTextField and Integer are incompatible types so one can't be assigned to the other. You want to set the content of the `JTextField. Replace

    wall2Area = Integer.valueOf(wall2Width.getText()) *
                  integer.valueOf(wall2Heighth.getText());
    

    with

    wall2Area.setText(Integer.toString(Integer.valueOf(wall2Width.getText()) * Integer.valueOf(wall2Heighth.getText())));
    

    Also, replace

    Integer.parseInteger(wall2Width.getText())
    

    with

    Integer.parseInt(wall2Width.getText())
    

    Always check the docs.