Question: Hello, this Java program is supposed to be a basic shopping thingy. I'm just going to write the pseudo code for it, to clear up

Hello, this Java program is supposed to be a basic shopping thingy. I'm just going to write the pseudo code for it, to clear up a concept. Basically, the customer has to type the name of the product they want and the program has to iterate through the inventory of products to see if there's a match or not. Pseudocode: for(int i=0; i < inventory.length; i++){ if(userInput == currentInventoryItem){ printOut: "item found and product number is i" return 1;} else if(i == inventory.length-1 && (userInput != currentInventoryItem)){ PrintOut ("Item not found"); return -1;} } } Someone suggested to me to delete the else if and just put the "product not found" message and the return -1 outside the for loop, which worked! I don't understand why my code didn't work. I also tried using products.length in my elseif statement to no avail. Deleting the elseif altogether is more concise, but I want to know why it won't work at all

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!