shavinda
shavinda

Reputation: 257

Java multi dimensional array

I want to search a multi-dimensional array and print the numbers greater than 7 with their locations.

This code compiles and runs without any errors, but doesn't provide any output.

Please help me to solve this problem.

class Sarr{

   public static void main(String args[]){    
     int[][] numArray = {{1,2,5,6,4,0},{6,0,1,2},{1,7,3,4},{3,5,6,8,5}};      
     arr(numArray);
   }

   private static void arr(int [][] array){

   int val = 7;

   for (int r = 0; r < array.length; r++) {
        for (int c = 0; c < array[r].length; c++) {

          if (array[r][c] > val){

             System.out.println("Value found was " + val + "["+r+"]"+"["+c+"]");

           }
        }            
    }
  }    
}

Upvotes: 0

Views: 444

Answers (3)

Belinda
Belinda

Reputation: 1231

The problem is that there is no number greater than 7 in your array. If you want it to print 7's you will need to change your if statement to

if(array[r][c]>=val) {
    //Print
}

Upvotes: 1

Shaded
Shaded

Reputation: 17826

It's because you're looking for strictly array[r][c] > 7 none of the values in your array are greater than 7.

Upvotes: 1

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Your test array does not have any element which is > 7 ...

Upvotes: 7

Related Questions