0

I am trying to make my code more modular. My question is, once you get the input by calling the getFile method, how can you save it to say an array, etc.?

To be more clear, I want to be able to call the getFileScanner method and it return whatever is on the file the user enters. Then within the main method, I want to be able to set that input (whatever was on the .txt file) to an array(to be written). How can I go about saving the input in the main method? On another post separate from this one, a user suggested I make my code more modular and suggested some of the below code. I am just trying to understand what the user had intended and split some of the 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 the 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;
    }

}
curiousX
  • 119
  • 7
  • It is not clear from your question or program what it is you are trying to do. You are constructing a `Scanner` for stdin, reading a `String` from the input, interpreting that as a path, opening that `File`, constructing a new `Scanner` for that file, and then printing the scanner object (twice). Love to help, but no clue what it is you are hoping to accomplish (yet). – AJNeufeld Jul 15 '16 at 21:10
  • you can find a solution here http://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner-class-in-java – YCF_L Jul 15 '16 at 21:12
  • @AJNeufeld edited my post. thanks for the feedback. – curiousX Jul 15 '16 at 21:18

1 Answers1

0

Making code more modular means breaking up the code into smaller, self contained, possibly reusable pieces. Something 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 is a more "modular" approach. Each method does one well-defined thing.

But your question is really "how do I read the file into an 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()]);
    }
AJNeufeld
  • 8,231
  • 1
  • 22
  • 40