0

Possible Duplicate:
How do I compare strings in Java?
String Comparison in Java…?

I'm working on what I though was a very simple problem...
Basic 101 shopping cart with product list hard coded (practice question only - I understand this is not how it would really be done)

I want the user to enter a string - a product code and then get the description of the associated product back from the Description method

It is returning a 0 value i.e. the if statements in the methods don't seem to recognise the user entered String.

The hard coded strings work - the keyboard entered string does not I am stumped but I think I'm just missing something fundamental

import java.util.*;

class CW1ShoppingCart1_2ShowProdAndGetChoice
{
public static void main(String[] args)

{
/*PART 1 Offer catalogue and get user choice*/

System.out.println("ITEMS AVAIALBLE TODAY: \n");
System.out.print("P4383"+ "\t"+ CW1ShoppingCart1GetProductDetails.Description("P4383"));
System.out.println("\t"+ "$"+ CW1ShoppingCart1GetProductDetails.Price("P4383"));
System.out.print("P4385"+ "\t"+ CW1ShoppingCart1GetProductDetails.Description("P4385"));
System.out.println("\t"+ "$"+ CW1ShoppingCart1GetProductDetails.Price("P4385"));
System.out.print("P4387"+ "\t"+ CW1ShoppingCart1GetProductDetails.Description("P4387"));
System.out.println("\t"+ "$"+ CW1ShoppingCart1GetProductDetails.Price("P4387"));
System.out.println("\nTO START SHOPPING ENTER A PRODUCT CODE AND HIT RETURN \n");

Scanner in = new Scanner (System.in);
String ProdCode =in.nextLine();

System.out.println("You Chose: "+ CW1ShoppingCart1GetProductDetails.Description(ProdCode));

}
}

class CW1ShoppingCart1GetProductDetails
{

static String Description(String ProdCode)
{

String Proddesc;

if(ProdCode=="P4387")Proddesc = "Little used helper monkey - 1 ";
else if(ProdCode=="P4385") Proddesc = "Chilli chocolate - 100g ";
else if(ProdCode=="P4383")  Proddesc = "State-owned Bank - real value - 1 entity ";
else Proddesc = "0";

return Proddesc;
}

static double Price(String ProdCode)
{

double ProdPrice;

if(ProdCode=="P4387")  ProdPrice = 1200;
else if(ProdCode=="P4385") ProdPrice = 3.27;
else if(ProdCode=="P4383")  ProdPrice = -0.08;
else ProdPrice = 0;

return ProdPrice;
}

} 
Community
  • 1
  • 1

3 Answers3

10

The problem is that you are using == to compare strings. Use equals instead.

if(ProdCode.equals("P4387"))Proddesc = "Little used helper monkey - 1 ";

and so on

gefei
  • 18,922
  • 9
  • 50
  • 67
0

Donot use == to compare strings.Sometimes you might be getting the correct result using '==' also, but that is due to same string existing in string pool.Always use equals method available in String class to compare string

Renjith
  • 3,274
  • 19
  • 39
0

As alternative solution you can use enums

like next

public enum ShoppingCart {
    P4387(1200, "Little used helper monkey - 1 "),
    P4385(3.27, "Chilli chocolate - 100g "),
    P4383(-0.08, "State-owned Bank - real value - 1 entity ");

    private final double price;
    private final String description;

    private ShoppingCart(double price, String description) {
        this.price = price;
        this.description = description;
    }

    public double getPrice() {
        return price;
    }

    public String getDescription() {
        return description;
    }
}

use somethig like:

System.out.printf("Shopping card with name %s have price = %f and description %s",
        currentShoppingCart.name(),
        currentShoppingCart.getPrice(),
        currentShoppingCart.getDescription());
iMysak
  • 2,170
  • 1
  • 22
  • 35