import java.io.*; class UseCharListsA3 { // A program which reads and writes lists of characters // in their conventional form. // Has recursive list reading and writing methods public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); CharListA L1,L2; System.out.print("Enter first list of characters: "); L1 = readCharListA(in); System.out.print("Enter second list of characters: "); L2 = readCharListA(in); System.out.print("The lists are: "); printCharListA(L1); System.out.print(" and "); printCharListA(L2); System.out.println(); } public static void printCharListA(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 readCharListA(BufferedReader in) throws IOException { String line = in.readLine(); CharListA L=null; try { if(line.charAt(0)!='[') { System.out.print("Error - expected '[',"); System.out.println(" found '"+line.charAt(0)+"'"); } else if(line.charAt(1)!=']') L = new CharListA(line.charAt(1),readRestList(line,2)); } catch(StringIndexOutOfBoundsException e) { System.out.print("Error - unexpected "); System.out.println("end of line"); } return L; } private static CharListA readRestList(String line,int i) { if(line.charAt(i)==']') { if(++i!=line.length()) { System.out.print("Error - extra characters: \""); System.out.println(line.substring(i,line.length())+"\""); } return null; } else if(line.charAt(i)!=',') { System.out.print("Error - expected ']' or ','"); System.out.println(", found '"+line.charAt(i)+"'"); return null; } else return new CharListA(line.charAt(i+1),readRestList(line,i+2)); } }