java - My input only read three commands and does nothing with the ones it reads? -
i have write user interface takes in data command screen , uses access methods in linked list class. both in same file , node class written , compiles fine. if can me figure out why reads 3 commands, such dog, cat, p , says java.io.ioexception: stream closed. checked if adding spaces i.e. commanding dog cat, bird, p affected amount of lines read , didn't. exception same. tips appreciated.
public static void main(string[] args){ linkedlist link= new linkedlist(); int n=0; system.out.println("type command\n"); try{ bufferedreader in=new bufferedreader(new inputstreamreader(system.in)); s=in.readline(); while(in.readline()!=null){ s=in.readline(); char first=s.charat(0); int space= s.indexof(" "); while(space<=n){ if(first=='i'){ string w=s.substring(space); link.insert(w); } if(first=='d'){ string w=s.substring(space); link.delete(w); link.printlist(); } if(first=='f'){ string w=s.substring(space); link.find(w); link.printlist(); } if(first== 'p'){ link.printlist(); } n++; } in.close(); } }catch(exception e) {system.out.println("ack!: " + e);} }
stream closed error means trying write in after has been closed. format code this:
open in stream on line before try try { processing here ..... } catch(exception e) { .... } { close stream here }
the block run code needs run after running try/catch, put stream close there.
Comments
Post a Comment