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+"]");
}
}
}
}
}
asked Mar 9, 2011 at 14:38
shavinda
2571 gold badge3 silver badges7 bronze badges
3 Answers 3
Your test array does not have any element which is> 7 ...
answered Mar 9, 2011 at 14:42
Vincent Mimoun-Prat
28.6k16 gold badges85 silver badges126 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
}
answered Mar 9, 2011 at 14:43
Belinda
1,2272 gold badges14 silver badges25 bronze badges
Comments
It's because you're looking for strictly array[r][c] > 7 none of the values in your array are greater than 7.
answered Mar 9, 2011 at 14:43
Shaded
18k8 gold badges41 silver badges63 bronze badges
Comments
lang-java
Value found was 7[3][3]