java.util.scanner - Cannot able to input String after Int in Java -
i inputing k , choice (integers) in main class. , trying input title_name in count class. ,i not able input title_name.
//main class
package com.iiitd.ap.lab6; import java.io.ioexception; import java.util.scanner; public class main { static int k; static int option; public static void main(string[] args) throws ioexception { scanner in = new scanner(system.in); k=in.nextint(); option=in.nextint(); in.close(); system.out.println(k+" "+option); count t=new count(k,option); t.count_print(); } }
//count class
package com.iiitd.ap.lab6; import java.io.bufferedreader; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstreamreader; import java.util.scanner; public class count { static int k; static int option; count(int k, int option) { count.k= k; count.option= option; } int decide_file_1() throws ioexception { string title_name=""; scanner in=new scanner(system.in); title_name=in.next(); system.out.println("ttt"); in.close(); for(int i=1;i<=20;++i){ fileinputstream fs= new fileinputstream("/home/tarun/downloads/lab6/papers/paper"+i+".txt"); bufferedreader br = new bufferedreader(new inputstreamreader(fs)); system.out.println(title_name+" "+br.readline()); if(title_name.equals(br.readline())) return i; } return 0; } int count_print() throws ioexception { if(option==1) { system.out.printf("decide_file=%d",decide_file_1()); } return 0; } }
any kind of appreciated.
you closing standard input stream (system.in
) calling in.close()
.
from scanner#close() javadoc,
if scanner has not yet been closed if underlying readable implements closeable interface readable's close method invoked. if scanner closed invoking method have no effect.
this means invoke input stream's close() method, there closing stream , disconnecting connection between read console , java program.
so, cannot use system.in
stream anymore in program (doesn't matter if using new scanner() object or not).
Comments
Post a Comment