jstraj's blog

By jstraj, 9 years ago, In English

I want to know why my code is giving me a hard time! It is giving me ArrayIndexOutOfBounds Exception!! Please Help Please help me optimize it too :(

import java.lang.*; import java.util.Scanner;

public class mike_and_fun { public static void main(String args[]) { Scanner s=new Scanner(System.in); int n,m,q; int i,j,posi,posj; n=s.nextInt(); m=s.nextInt(); q=s.nextInt(); int a[][]=new int[n][m]; int b[][]=new int[q][2]; for(i=0;i<n;i++) { for(j=0;i<m;j++) { a[i][j]=s.nextInt(); } } for(i=0;i<q;i++) { for(j=0;j<2;j++) { b[i][j]=s.nextInt(); } }

    for(i=0;i<q;i++)
    {
       posi=b[i][0];
       posj=b[i][1];
       if(a[posi][posj]==1)
         a[posi][posj]=0;
       else
         a[posi][posj]=1;
       answer(a,n,m);
    }

}
public static void answer(int a[][],int n,int m)
{
    int i,j,ans=0,max=0;
    for(i=0;i<n;i++)
    {
       for(j=0;i<m;j++)
       {
         if(a[i][j]==1)
          ans++;
       }
       if(ans>max)
         max=ans;
       ans=0;
    }
    System.out.println(max);
}

}

  • Vote: I like it
  • -7
  • Vote: I do not like it

| Write comment?