java - Creating a void method that reads a file, that can be called by other methods in the class? -
public class dash { public void readfile(string fil) throws filenotfoundexception { try { scanner input = new scanner(new file(fil)); string text = input.next(); input.close(); } catch (filenotfoundexception e) { } } public int getnumdashes() throws filenotfoundexception { readfile(fil); string text = input.next(); // code find number of dashes string of read file. } }
as indicated in title, read file in 1 method. since cannot return string readfile
method, have obtain string within getnumdashes
method. however, i'm not sure how this. second method's return value based of off whatever file name passed readfile()
.
you use non-static field string, store text input file.
public class dash { string text; public void readfile(string fil) throws filenotfoundexception { try{ scanner input = new scanner (new file(fil)); text= input.next(); input.close(); } catch (filenotfoundexception e){ } public int getnumdashes() throws filenotfoundexception { readfile(fil); // can read field *text* here , process input // code find number of dashes string of read file. } }
Comments
Post a Comment