public class Runner {
static NewArray2D a = new NewArray2D();
protected static int rows;
protected static String val;
protected static int r;
protected static int c;
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter Number of records:");
rows=in.nextInt();
a.create(rows, 5);
System.out.println();
a.put(0, 0, (String) "Customers Name");
a.put(0, 1, (String) "Brand Preffered");
a.put(0, 2, (String) "Avg. Monthly shopping Bill");
a.put(0, 3, (String) "Monthly Income");
a.put(0, 4, (String) "Monthly Expenses");
for( r=1; r<rows; r++){
for( c=0; c<5; c++){
System.out.println("Enter " + c +" record");
val=in.next();
if(c<2 && Character.isLetter(val.charAt(0)))
{a.put(r, c, val);System.out.println();}
else if(c<2 && Character.isDigit(val.charAt(0)))
{System.out.println("Invalid input");System.out.println();}
else if(c>=2 && Character.isDigit(val.charAt(0)))
{a.put(r, c, val);System.out.println();}
else
{System.out.println("Invalid input");}
}
}
System.out.println(a.toString());
System.out.println();
System.out.println("Do you want to change any block?:" + " " + "(Y/N)");
char choice=in.next().charAt(0);
if(choice=='Y'|| choice=='y'){
System.out.println("Which block do you want to edit:");
System.out.println("Row: "); r= in.nextInt();
System.out.println("Column: "); c= in.nextInt();
System.out.println("Value: "); val= in.next();
if(c<2 && Character.isLetter(val.charAt(0)))
{a.put(r, c,val );System.out.println();}
else if(c<2 && Character.isDigit(val.charAt(0)))
{System.out.println("Invalid input");
System.out.println();}
else if(c>=2 && Character.isDigit(val.charAt(0)))
{a.put(r, c, val);System.out.println();}
else
{System.out.println("Invalid input");}
}
if(choice=='N' || choice=='n')
{System.exit(0);}
System.out.println(a.toString());
in.close();
}
}
public class NewArray2D {
protected int tcols;
protected int trows;
protected Object[] elem;
public NewArray2D(int row,int col) {
if(row<1||col<1)throw new IllegalArgumentException();
elem=new Object[row*col];
tcols=col;
trows=row;
}
public NewArray2D() {
trows=tcols=0;
}
public void create(int row,int col) {
if(row<1||col<1)throw new IllegalArgumentException();
elem=new Object[row*col];
tcols=col;
trows=row;
}
public void checkIndex(int row,int col){
if(row<0||col<0)throw new IndexOutOfBoundsException();
}
public void put(int row,int col,Object telem){
checkIndex(row, col);
elem[row*tcols+col]=telem;return;
}
public Object get(int row,int col){
checkIndex(row, col);
return elem[row*tcols+col];
}
public Object remove(int row,int col){
checkIndex(row, col);
Object ob=elem[row*tcols+col];
elem[row*tcols+col]=null;
return ob;
}
public int nrows(){return trows;}
public int ncols(){return tcols;}
public String toString(){
StringBuffer s= new StringBuffer("[");
for(int r=0;r<trows;r++){
for(int c=0;c<tcols;c++){
if(elem[r*tcols+c]==null)s.append("_, ");
else s.append(elem[r*tcols+c].toString()+", ");
}
s.delete(s.length()-2, s.length());
s.append(" # ");
}
if(trows>0&&tcols>0)
s.delete(s.length()-2, s.length());
s.append("]");
return new String (s);
}
}