java - How to read a txt file as my system.in using eclipse -
i'm working through problems on code chef. i'm stuck problem , says have wrong answer. want test program see output reads input text file , can't figure out how eclipse, code below:
import java.io.*; class holes { public static void main(string[] args) throws ioexception{ // todo auto-generated method stub bufferedreader r = new bufferedreader(new inputstreamreader(system.in)); int testcases = integer.parseint(r.readline()); (int =0; i<testcases; i++) { int holes = 0; string s = r.readline(); (int j= 0; j< s.length(); j++) { char c = s.charat(j); if (c == 'b') holes += 2; else if (c== 'a' || c== 'd' ||c== 'o' ||c== 'p' ||c== 'q' ||c== 'r' ) { holes +=1; } system.out.println(holes); } } } }
add folder eclipse project in folder add input file , read using bufferreader follows bufferedreader br = null;
try { string scurrentline; br = new bufferedreader(new filereader("yourfolder/theinputfile.txt")); while ((scurrentline = br.readline()) != null) { system.out.println(scurrentline); } } catch (ioexception e) { e.printstacktrace(); } { try { if (br != null)br.close(); } catch (ioexception ex) { ex.printstacktrace(); } }
this 1 way other way pass path argument program shown bellow
try { string scurrentline; br = new bufferedreader(new filereader(args[0])); while ((scurrentline = br.readline()) != null) { system.out.println(scurrentline); } } catch (ioexception e) { e.printstacktrace(); } { try { if (br != null)br.close(); } catch (ioexception ex) { ex.printstacktrace(); } }
how when run application run configuration , there find args can add ever path in example c:\myinput.txt
Comments
Post a Comment