java - No such Element exception for matrix of integers? -


though code seems small , simple throwing no such element exception..the question take matrix of integers , should check if there elements around elements less element. , have replace element 'x'.no need check border elements... new java...so corrections appreciated!!

import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*;  public class solution {  public static void main(string[] args) {      scanner in = new scanner(system.in);     int n= in.nextint();     int a[][] = new int[n][n];     for(int i=0;i<n;i++) //getting input user         {         for(int j=0;j<n;j++)             {             a[i][j] = in.nextint();         }     }     for(int i=0;i<n;i++) // checking condition         {         for(int j=0;j<n;j++)             {             if(i!=0 && i!=n-1 && j!=0 && j!=n-1)                 {                 if(a[i-1][j]<a[i][j] && a[i+1][j]<a[i][j] && a[i][j-1]<a[i][j] && a[i][j+1]<a[i][j])                     {                     system.out.println("x ");                 }                 else                     {                     system.out.println(a[i][j]+" ");                 }              }         }         system.out.println();     } } }  input:           output: 4                  1112              1112     1912              1x12 1892              18x2  1234              1234    

your if condition if(i!=0 && i!=n-1 && j!=0 && j!=n-1) should changed to...

if(i>0 && i<n-1 && j>0 && j<n-1) 

and fine. try resolve index of arrays not exceed limit, in case happens when a[i+1][j]<a[i][j] or a[i][j+1]<a[i][j] called.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -