-1

I'm making a cash register.

I would like to set the text of the label (cost) when the text in the textfield (receipt) is "chocolate".

private void dodaj_produktActionPerformed(java.awt.event.ActionEvent evt) 
 {                                              
   product_name.getText();
   String str = product_name.getText();
   receipt.setText(str);
   String chocolate="chocolate";

   if(str == chocolate){
       Double cena=3.50;
       String cena_tabliczka = Double.toString(cena);
       cost.setText(cena_tabliczka);
   }

 }
Bug Hunter Zoro
  • 1,743
  • 18
  • 21

1 Answers1

0

You are comparing incorrectly. In Java you need to use the .equals() operator for comparing objects.

Instead of:

if(str == chocolate){

Try this:

if(str.equals(chocolate)){

Or if you don't care about case

if(str.equalsIgnoreCase(chocolate)){
buræquete
  • 14,226
  • 4
  • 44
  • 89
purring pigeon
  • 4,141
  • 5
  • 35
  • 68