user1462585
user1462585

Reputation: 37

Java 2D array for loop not reading the last value

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
    }

Upvotes: 1

Views: 1747

Answers (3)

irrelephant
irrelephant

Reputation: 4111

Try this:

while (arrayRow < invArray.length) {
    for (arrayCol = 0; arrayCol < invArray[arrayRow].length; arrayCol++) {
        //...
    }
    arrayRow++;
}

Your array has dimensions [4][5]; you're switching the two and trying to access an element as if the array had dimensions [5][4]. You're also only accessing 16 elements since invArray[a].length will always be 4 for all a that work.

Upvotes: 1

Dims
Dims

Reputation: 51069

You have wrong second loop. It should be

for (arrayRow = 0; arrayRow < invArray.length; arrayRow++){

since arrayRow is the first index. In you code you get 4 instead of 5 which causes an error.

Also you would better use for for both loops.

And also you should better have column loop inside row loop, since former is dependent, or you should exchange indices;

Upvotes: 3

AlexWien
AlexWien

Reputation: 28737

are you sure that you need the second line

itemName = "Jackets";
arrayCol++;

bur your code confuses me a bit, so i am not sure if this solves.

Upvotes: 0

Related Questions