so far 'm abl to write code upto this.
condition 3 needs solution in this, i read other soutions but unable to understand them.
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> l = new ArrayList<String>();
for(int i=0; i<5; i++)
{
String s = b.readLine();
l.add(i);
}