-2

I need my program to set the text in the textField_price to the price of the product, but all I get is error. It seems p.getPrice() does not work but I have no idea how to fix it??? Have tried a bunch of ways... I understand it's the setText that is wrong, since I'm dealing with a double not a String, but I have no clue what to do???

    String productName = textField_productName.getText();

    Product p = controller.findProduct(productName);
    if (productName.isEmpty())
        textArea_product.setText("Vänlig fyll i produktnamn.");

    else if (p != null) {
        textField_productName.setText(p.getProductName());
        textField_kategory.setText(p.getKategory())
        textField_price.setText(p.getPrice());

        textArea_product.setText("Produkten " + p.getProductName().toUpperCase() + " hittad.");
    } else {
        textField_price.setText("---");
        textField_kategory.setText("---");
        textArea_product.setText("Produkten med produktnamnet " +  productName  + " hittas ej");
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • "but all I get is error" "does not work" doesn't really tell us anything about problems you are getting, please take a look at http://importblogkit.com/2015/07/does-not-work/. "Have tried a bunch of ways..." is similar, we don't know what exactly did you try and what happened. Maybe you already used suggested approach but there was some other error related to code which you didn't show. – Pshemo Dec 24 '17 at 11:42

3 Answers3

0

You can convert the number to string before setting it:

textField_price.setText(String.valueOf(p.getPrice()));

ernest_k
  • 44,416
  • 5
  • 53
  • 99
0

You need to cast double to String

textField_price.setText(String.valueOf(p.getPrice()));
Yousaf
  • 27,861
  • 6
  • 44
  • 69
0

First cast your double variable to string like this:

textField_price.setText(String.valueOf(p.getPrice()));
Lalit Verma
  • 782
  • 10
  • 25