import java.io.*; class CharListAIO { public static void print(CharListA L) { System.out.print("["); if(L!=null) { System.out.print(L.head); printRestList(L.tail); } System.out.print("]"); } private static void printRestList(CharListA L) { if(L!=null) { System.out.print(","+L.head); printRestList(L.tail); } } public static CharListA read(BufferedReader in) throws IOException,ListFormatException { String excmess,line = in.readLine(); CharListA L=null; try { if(line.charAt(0)!='[') { excmess="Expected '[', found '"+line.charAt(0)+"'"; throw new ListFormatException(excmess); } else if(line.charAt(1)!=']') L = new CharListA(line.charAt(1),readRestList(line,2)); } catch(StringIndexOutOfBoundsException e) { excmess="Unexpected end of line"; throw new ListFormatException(excmess); } return L; } private static CharListA readRestList(String line,int i) throws ListFormatException { String excmess; if(line.charAt(i)==']') { if(++i!=line.length()) { excmess="Extra characters: \""; excmess+=line.substring(i,line.length())+"\""; throw new ListFormatException(excmess); } return null; } else if(line.charAt(i)!=',') { excmess="Expected ']' or ','"; excmess+=", found '"+line.charAt(i)+"'"; throw new ListFormatException(excmess); } else return new CharListA(line.charAt(i+1),readRestList(line,i+2)); } }