java - scanner class more modular -


i trying make code more modular. question is, once input calling getfile method, how can save array, etc.?

to more clear, want able call getfilescanner method , return whatever on file user enters. within main method, want able set input (whatever on .txt file) array(to written). how can go saving input in main method? on post separate one, user suggested make code more modular , suggested of below code. trying understand user had intended , split of code up.

import java.io.file ; import java.io.filenotfoundexception ; import java.util.scanner ;  public class scantest {      public static void main(string[] args) throws filenotfoundexception {          system.out.println("please enter file");         system.out.println(getfilescanner());      }      public static scanner getfilescanner() throws filenotfoundexception {          scanner user_input = new scanner(system.in);         string filepath = user_input.next();          system.out.println("filepath read: " + filepath);         // read input file         scanner input = new scanner(new file(filepath));         system.out.println(input);         return input;     }  } 

making code more modular means breaking code smaller, self contained, possibly reusable pieces. like:

public class scantest {     public static void main(string []args) throws filenotfoundexception {         scanner user_input = new scanner(system.in);         string filepath = getfilepath(user_input);         string[] all_lines = readalllines(filepath );     }      public static string getfilepath(scanner user_input) {         string filepath = user_input.next();         system.out.println("filepath read: " + filepath);         return filepath;     }      public static string[] readalllines(string filepath) throws filenotfoundexception {         // todo: implement     } } 

this more "modular" approach. each method 1 well-defined thing.

but question "how read file array."

    public static string[] readalllines(string filepath) throws ioexception {         list<string> lines = files.readalllines(paths.get(filepath), standardcharsets.utf_8);         return lines.toarray(new string[list.size()]);     } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -