Tuesday, February 1, 2011

pattern






/**
* bhatt.umang7@gmail.com
* @author Umang B Bhatt
* if a number is repeated more than 3 times then exception should be thrown
*/

class
MyException extends RuntimeException
{


public
MyException(String message)
{

super(message);
}
}


public class
test

{


public static
void main(String args[])
{

if
(args.length > 2)
{


// do something
System.out.print("Invalid number of arguments. More than one arguments were give.\nOnly onw was expected");
System.exit(0);
}


else if
(args.length != 1)
{

System.out.print("You must pass some argument");

System.exit(0);
}

String st = args[0];

for
(int i = 0; i < st.length(); i++)
{


if
(Character.isDigit(st.charAt(i)) == false)
{


System.out.print("All the arguments needs to be numeric");
System.exit(0);
}
}


// now getting the array with values
char arr[][] = new char[10][3];

int
val = 65;
for
(int i = 0; i < 3; i++)
{


for
(int j = 0; j < 10; j++)
{


arr[j][i] =(char) val;
val++;
}
}


arr[6][2] = '@' ;
arr[7][2] = '$' ;

arr[8][2] = '#' ;
arr[9][2] = '*' ;

for
(int i = 0 ; i < st.length() ; i++)
{


char
c = st.charAt(i);
int
t = c -48;

if
(arr[t][0]=='1')
{

throw
new MyException("There were more than 3 accurance of same number");
}


else

{

System.out.print(arr[t][0]);

arr[t][0] = arr[t][1];
arr[t][1] = arr[t][2];

arr[t][2] = '1';
}
}

System.out.println();

}
}