0

This is what i have, i just pass in the filename in this, but i want to be able to get the filename from the user.

try {
        Scanner inFile = new Scanner (Paths.get("PostalCodeRecord.txt"));
        while (inFile.hasNextLine()) {
            String dataLine = inFile.nextLine();
            if (dataLine.length()>6){
                Scanner line = new Scanner (dataLine);
                String month = line.next();
Zyko
  • 3
  • 2
  • For a GUI thing, try with JFileChooser.showOpenDialog(null) – xs0 Jan 23 '19 at 19:36
  • You can create a `Scanner` to read from the console and then save the filename to a variable. Take a look at the example in the answer to a [similar question](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java/31241089). – Wrokar Jan 23 '19 at 19:37

1 Answers1

1

You need to create another Scanner object and ask for the file name from the user and store it in a string variable. Then you use that in your file scanner

 try {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter file name");
    String fileName = scan.nextLine();
    Scanner inFile = new Scanner (Paths.get(fileName));
    while (inFile.hasNextLine()) {
        String dataLine = inFile.nextLine();
        if (dataLine.length()>6){
            Scanner line = new Scanner (dataLine);
            String month = line.next();
user2023608
  • 468
  • 5
  • 16