0

I wrote program .. the user must enter a string with format (Barcode,price:number of item) and I calculate the total of barcode

my code is

import java.util.*;

public class hanoofee2 {
    public static void main (String args[]) {
        Scanner input=new Scanner (System.in);

        System.out.println("Enter a string with format (Barcode,price:number of item) ");
        String ent = input.nextLine();
        String barcode = ent.substring(0,(ent.indexOf(",")));
        String price = ent.substring((ent.indexOf(","))+1,(ent.indexOf(":")));
        String number = ent.substring(ent.indexOf(":")+1,(ent.length()));

        double price2 = Double.parseDouble(price);
        int number2 = Integer.parseInt(number);
        double total = price2 * number2;

        String m = "";
        if(barcode == "11A") {
            m ="green pen";
        } else if(barcode == "22B") {
            m ="printer";
        } else if(barcode == "44C") {
            m ="Memory card";
        } else if(barcode == "44D") {
            m ="Cook book";
        }

        System.out.printf("The total price of%2s "+" is:%.2f ", m ,total);
    }
}

every things are ok but when I press run the value of " m" does not display

     Enter a string with format (Barcode,price:number of item) 
       22B,222:30
       The total price of    is:6660.00 
        ----jGRASP: operation complete.

what is the problem ?!

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
hanooee
  • 5
  • 1
  • 3

1 Answers1

2

every things are ok but when I press run the value of " m" does not display

Don't use == to compare content of String.

Use equals() instead.

Why should I use equals() instead of == ?

Community
  • 1
  • 1
Alexis C.
  • 91,686
  • 21
  • 171
  • 177