Question

In: Computer Science

Why does this code not work even when typing in the correct product code? It always...

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

Solutions

Expert Solution

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:

  1. Main difference between .equals() method and == operator is that one is method and other is operator.
  2. We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
  3. If a class does not override the equals method, then by default it uses equals(Object o) method of the closest parent class that has overridden this method.

public class Test {

    public static void main(String[] args)

    {

        Thread t1 = new Thread();

        Thread t2 = new Thread();

        Thread t3 = t1;

        String s1 = new String("GEEKS");

        String s2 = new String("GEEKS");

        System.out.println(t1 == t3);

        System.out.println(t1 == t2);

        System.out.println(s1 == s2);

        System.out.println(t1.equals(t2));

        System.out.println(s1.equals(s2));

    }

}

Output:

true
false
false
false
true

Related Solutions

Your work should be readable as well as correct. Always Verify Time! Write code for O(n)...
Your work should be readable as well as correct. Always Verify Time! Write code for O(n) worst-case algorithm that verifies that a tree is actually an AVL tree by detecting imbalance. If imbalance is true, then call the proper rotation function provided in the lecture slides to fix the imbalance condition. 1. Must read AVLNode data from a text file 2. Create a Book Object; and an AVL node object to be inserted into the AVL tree 3. At each...
Your work should be readable as well as correct. Always Verify Time! Write code for O(n)...
Your work should be readable as well as correct. Always Verify Time! Write code for O(n) worst-case algorithm that verifies that a tree is actually an AVL tree by detecting imbalance. If imbalance is true, then call the proper rotation function provided in the lecture slides to fix the imbalance condition. 1. Must read AVLNode data from a text file 2. Create a Book Object; and an AVL node object to be inserted into the AVL tree 3. At each...
Explain why market prices will always converge to their fundamental value even when we disregard the...
Explain why market prices will always converge to their fundamental value even when we disregard the efficient market hypothesis.
why does the wiedemann franz law always work at high temperatures? why are the scattering processes...
why does the wiedemann franz law always work at high temperatures? why are the scattering processes simular at that range, and what would happen if a electric field is applied
Why does the Zaitsev product always form in an E1 reaction regardless of the bulkiness of...
Why does the Zaitsev product always form in an E1 reaction regardless of the bulkiness of the base?
How does the blood typing test work? If provided with a hypothetical situation, be able to...
How does the blood typing test work? If provided with a hypothetical situation, be able to predict the results.
In all algorithm, always explain how and why they work. ALWAYS, analyze the complexity of your...
In all algorithm, always explain how and why they work. ALWAYS, analyze the complexity of your algorithms. In all algorithms, always try to get the fastest possible. A correct algorithm with slow running time may not get full credit. In all data structures, try to minimize as much as possible the running time of any operation. 1.Show that if a binary tree has a vertex with a single child, then this can not be an optimal Huffman tree. 2. Question...
When you go in for a cancer screening, they are not always correct. Sometimes you get...
When you go in for a cancer screening, they are not always correct. Sometimes you get screened for cancer and the initial results come back that you do not have cancer when actually you do have cancer (false negative), or maybe on your initial cancer screening you get tested positive for cancer but then actually you don’t have cancer (false positive). You could display this as a hypothesis test and the false positive and false negative are a Type I...
Committing large investments to a single project is always risky, and it becomes even riskier when...
Committing large investments to a single project is always risky, and it becomes even riskier when a competitor is set to do the same thing and the market is not ready to sustain two rival products.  In the game called "The Innovator's Dilemma" (or "The Developer's Dilemma"), there are two identical firms, A and B, and each can make one of two choices, to invest in the development of a new product or not to invest. If only one of the...
Instructions: Show all your work to get partial credit. Answers without any work-even correct-will not receive...
Instructions: Show all your work to get partial credit. Answers without any work-even correct-will not receive any credit. Good Luck! A firm is considering investing in a new project. According to its cost of capital while using debt, preferred stock and new common stock, the project is expected to have an initial after-tax cost of $5,000,000. Furthermore, the project is expected to provide after-tax operating cash flows of $1,800,000 in year 1, $2,900,000 in year 2, $2,700,000 in year 3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT