In: Computer Science
Why does this code not work even when typing in the correct product code? It always gives me the error message even though im typing one of the 3 correct product codes.
String[] product_code= new String[3];
String[] product_name= new String[3];
// String[] product_description = new String[3];
int[] quantity = new int[3];
double[] price = new double[3];
double[] itemTotal = new double[3];
double subTotal = 0, salesTax, total;
//getting all the needed inputs
for(int i = 0; i < 3; i++)
{
System.out.println("Input product code " + (i + 1) + ":");
product_code[i] = input.nextLine();
if(product_code[i]!= "100" || product_code[i]!= "101" ||
product_code[i]!= "102") {
System.out.println("Error: Invalid Product Code
please retry purchase ");
break;
}
I probably thing you are trying to validate the condition using'==' instead of "equals" method.Because as you declared the product_code as String data type you must be using euals method to consider the condition evaluation against the product_code values.
In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:
|
Output:
true false false false true