I'm working on a school assignment and I'm having an issue with a 2d array. Basically my program takes an integer from the the user and determines if one of the array values is lower than that integer. I have a while and for loop set up to do the trick, but I can't seem to get my loop to use the last row of data. I've tried mixing things around for a couple of hours now.
I'm sure I'm derping really badly on whatever I'm missing, but I could use a hand now. Here is the relevant data for my code
//array of store inventory
int invArray[][] = {{102, 54, 20, 78},
{45, 25, 35, 75},
{12, 35, 45, 65},
{54, 25, 34, 45},
{15, 35, 50, 25}};
int menuChoice = 0; //allows user to select a menu option
int storeNum=0; //store number
String itemName="null"; //name for an item
int itemAmount=0; //items below this amount are pulled up in choice 4
int arrayRow=0; //row value for invArray based on itemName
int arrayCol=0; //column value for invArray based on storeNum
//menu 4
if (menuChoice == 4){
System.out.print("Find all items below the number: ");
//catches mistakes
try{
itemAmount = input.nextInt();
}
catch(Exception e){
System.out.print("Invalid value entered. Try again: ");
itemAmount = input.nextInt();
}
System.out.println("Items below "+itemAmount+": ");
//loop that checks item values at all stores
arrayRow=0;
arrayCol=0; //counters for loop
while (arrayCol < invArray[arrayRow].length){
for (arrayRow = 0; arrayRow < invArray[arrayCol].length; arrayRow++){
if (invArray[arrayRow][arrayCol] < itemAmount){
if (arrayRow == 0)
itemName = "Tennis Shoes";
if (arrayRow == 1)
itemName = "Sweaters";
if (arrayRow == 2)
itemName = "Jeans";
if (arrayRow == 3)
itemName = "Shorts";
if (arrayRow == 4)
itemName = "Jackets";
//outputs stores with values lower than the one given
System.out.println("Store 10"+(arrayCol+1)+" "+itemName+": "
+invArray[arrayRow][arrayCol]);
for (int i = 0;invArray[4][i] < invArray[arrayRow].length; i++)
System.out.println("Store 10"+(i+1)+" "+" jackets : "+invArray[i][4]);
}
}
arrayCol++;
System.out.println();
}//end item check loop
}
while (arrayCol < invArray[arrayRow].length)bywhile (arrayCol < invArray.length)would make more sense. Not sure if that will solve your issue though.invArray[whatever].lengthwill be the number of columns ( = 4), not the number of rows ( = 5). Which means you'll only read 4 * 4 = 16 elements, and you won't get to the last row.