0

i really need help with this program i am finding a saddle number of a 2-d array which is the min value in a particular column and maximum value in its row.......and my error with the code is arrayindexoutofboundexception
eg
input num=4
7 6 9 10
5 3 4 6
8 2 4 8
9 5 4 8
output 6

import java.io.*;
public class sp
{
int i,saddle,num;int arr[][]=new int[num][num];BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takenum()throws IOException
{

int j;
System.out.println("ENTER SIZE");
num=Integer.parseInt(br.readLine());
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
System.out.print("NUMBERS");
arr[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("LIST");
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
System.out.print(arr[i][j]);
}
System.out.println();
}
}
public void showresult()throws IOException
{
int j,flag=0,minr=0,minc=0,k;
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(j==0)
{
minr=arr[i][j];
minc=j;
}
else if(arr[i][j]<minr)
{
minr=arr[i][j];
minc=j;
}
}
for(k=0;k<num;k++)
{
if(minr<arr[k][minc])
{
break;
}}
if(k==num)
{
flag=1;
saddle=minr;
}
}
if(flag!=0)
System.out.print("SP="+saddle);
else
System.out.print("NSP");
}
public void main()throws IOException
{
sp obj=new sp();
obj.takenum();obj.showresult();}}

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by desert564
1

The main method should be written as follows:

public static void main(String args[])throws IOException
{
sp obj=new sp();
obj.takenum();obj.showresult();
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.