import java.io.*; class CharListBIO { public static void print(CharListB L) { System.out.print("["); if(L!=null) { System.out.print(L.head()); printRestList(L.tail()); } System.out.print("]"); } private static void printRestList(CharListB L) { if(L!=null) { System.out.print(","+L.head()); printRestList(L.tail()); } } public static CharListB read(BufferedReader in) throws IOException,ListFormatException { String excmess,line = in.readLine(); CharListB L=null; try { if(line.charAt(0)!='[') { excmess="Expected '[', found '"+line.charAt(0)+"'"; throw new ListFormatException(excmess); } else if(line.charAt(1)!=']') L = new CharListB(line.charAt(1),readRestList(line,2)); } catch(StringIndexOutOfBoundsException e) { excmess="Unexpected end of line"; throw new ListFormatException(excmess); } return L; } private static CharListB 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 CharListB(line.charAt(i+1),readRestList(line,i+2)); } }