import java.io.*; class UseCharListsA2 { // A program which reads and writes lists of characters // in their conventional form. 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); for(CharListA ptr=L.tail;ptr!=null;ptr=ptr.tail) System.out.print(","+ptr.head); } System.out.print("]"); } public static CharListA readCharListA(BufferedReader in) throws IOException { String line = in.readLine(); int i=0; CharListA L=null; try { if(line.charAt(i)!='[') { System.out.print("Error - expected '[',"); System.out.println(" found '"+line.charAt(i)+"'"); return null; } if(line.charAt(++i)==']') return null; L = new CharListA(line.charAt(i++),null); for(CharListA ptr=L; line.charAt(i++)==',' ;ptr=ptr.tail) ptr.tail = new CharListA(line.charAt(i++),null); if(line.charAt(i-1)!=']') { System.out.print("Error - expected ']' or ','"); System.out.println(", found '"+line.charAt(i-1)+"'"); } if(i!=line.length()) { System.out.print("Error - extra characters: \""); System.out.println( line.substring(i,line.length())+"\""); } } catch(StringIndexOutOfBoundsException e) { System.out.print("Error - unexpected "); System.out.println("end of line"); } return L; } }