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;
}
}