1

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
    }
4
  • Replacing while (arrayCol < invArray[arrayRow].length) by while (arrayCol < invArray.length) would make more sense. Not sure if that will solve your issue though. Commented Dec 9, 2012 at 23:39
  • 1
    invArray[whatever].length will 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. Commented Dec 9, 2012 at 23:45
  • Whenever I remove the [] fields from either .lengths I get an out of bounds error Commented Dec 9, 2012 at 23:50
  • @user1462585 Switch the variables arrayCol and arrayRow only in your for loops. Commented Dec 9, 2012 at 23:52

3 Answers 3

3

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;

Sign up to request clarification or add additional context in comments.

1 Comment

Did exactly what you suggested and it worked! Just needed to add an exception for an out of bounds error. I wanted to keep the format as rows inside columns so I could list the item quantity at each particular store, but I'ld rather have the thing work! Thanks for your help
1

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.

Comments

0

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.

1 Comment

I was trying all sorts of random things to get the 5th line to read. I guess I didn't clean my code up all the way before I posted it here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.