Need help with a loop
Hi guys, I have an array of stock, which has variables - part numbers, stock quantity etc and I am trying to issue stock from the array. I am using a method issueStock(), the method has no parameters. In the method i am asking the user to input the part number of the item they wish to issue in turn i am searching the array comparing the user's inputed data to part numbers in the array. I am having trouble using a for loop when the part number is not found, I am using if statements inside the for loop but with no success.
Any ideas? I am only new to this and am not sure if the above query is even making sense. Any feedback would be great. :)
Posting code can usually help bring sense to posts like this. What is the declaration of your array? What type of objects are in it? What is the class for those objects (if applicable)? What have you tried--and what isn't working about your attempts?
Scanner sc = new Scanner(System.in);
System.out.println("Please enter part code you wish to issue");
int d = sc.nextInt();
for(int i = 0; i < stores.length; i++)
if(d == stores[i].getPartCode())
System.out.println("Please enter number of stock you wish to issue");
int n = sc.nextInt();
else if(d != stores[i].getPartCode())
System.out.println("Part code entered is wrong");
When the right part code is entered the for loop does not stop it keeps running for the length of the stores array, I have noticed that by the end of the loop it will always print out "Part code entered is wrong" if I enter the first part code in the array, "Part code entered is wrong" will print out 7 times (there is 8 items in the array). The array is filled already with cleaning, stationary and food items, the user does not enter these into the array.
also I have tried a Do - While and a While loop with no luck,
Thanks Zach for the reply I hope this information will help you help me.