Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

David Hinton
David Hinton
3,070 Points

Maths in java

Hi i'm having a few issues with a program i'm trying to run.

I want to be able to set an area

Then be able to set a length and width of a product then have the area divided by the size of the product and tell me how many I would need.

I'm still new to this and struggle sometimes with my brain.

So I set my variables

int answer;
int length;
int width;
int size;  
int result;

I assumed I would have this in the submit button code

private void buttonSubActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String answer;
    area = areaTxt.getText(); // I get the string can't be converted to int error
    size = lengthtxt + widthtxt;   // I get the bad operator '+' error
    result = area % size;
    answer = String.format("Boards needed ",result);
    resultsTxt.setText(answer);
    // TODO add your handling code here:
}  

I get the bad operator '+' error and

1 Answer

John Sns
John Sns
4,193 Points

use Integer.parseInt("A string with integer value.")

area = Integer.parseInt(areaTxt.getText());//That should clear up this problem
//I would need more information about the initialization(or declaration) of lengthtxt and width text to correct the other error.
David Hinton
David Hinton
3,070 Points

Hi thanks for your reply I had a play around and got a bit further

    length = Integer.parseInt(lengthtxt.getText());
    width = Integer.parseInt(widthtxt.getText());

    size1 = width + length;

    answer = area /size1;
    Integer.parseInt(resultsTxt.setText(answer));  // int cannot be converted to string error

I get that error even though I use the parseInt