0

I am attempting to learn how to prompt users to enter a file name, rather than predefining it, so that Java can go through with the Scanner and read through it. I have written a program that does what I need it to do, but only when the file in question is predefined.

I have looked around the site for duplicate questions, and found a few, but the scope in which it was asked was a bit more advanced than where I am. Can anyone offer me a bit of guidance on how to prompt a user to enter the file he/she wants, as opposed to predefining it (as code below is set to).

Note - This was written assuming the files read in had integers < 0 and > 0, hence why the min/max functions were done in the way they were...simply trying to teach myself one step at a time.

import java.io.*;
import java.util.*;

public class ProcessNumbers {

public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("inputdata.txt"));
    int max = 0;
    int min = 0;
    int sum = 0;
    int count = 0;
    double average = 0;

    System.out.println("Enter file name: ");  //currently a println for ease of reading via Run

    while (input.hasNext()) {
        if (input.hasNextInt()) {
            int number = input.nextInt();
            sum += number;
            count++;
            average = (double) (sum) / count;
            if (number > max) {
                max = number;
            }
            if (number < min) {
                min = number;
            }
        } else {
                input.next();   
            }
    }

    System.out.println("Maximum = " + max);
    System.out.println("Minimum = " + min);        
    System.out.println("Sum = " + sum);
    System.out.println("Count = " + count);
    System.out.println("average = " + average);
}

}
Rivers31334
  • 644
  • 1
  • 12
  • 30
  • the accepted answer here shows how to get a string from the user : http://stackoverflow.com/questions/5287538/how-to-get-basic-user-input-for-java you can use this to create your File object – Graham Griffiths Mar 03 '14 at 16:14

4 Answers4

1

Try this:

System.out.println("Enter file name: ");
Scanner fileNameScanner = new Scanner( System.in );
String fileName = "";

if ( fileNameScanner .hasNext() ) {
    fileName = fileNameScanner.next();
}
...

Using the fileName string, create a File object and use as per your requirements.

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
0

Easiest way would be to replace

new File("inputdata.txt")

with

new File(args[0])

This way the first command-line argument will be treated as a filename.

sjr
  • 9,769
  • 1
  • 25
  • 36
0

You can use Scanner to read a input from user (it doesn't only read from a File):

Scanner prompt = new Scanner(System.in);

System.out.print("Enter name of the file: ");
String name = prompt.next(); // enter "inputdata.txt"

Scanner input = new Scanner(new File(name));
// ...
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

Another approach would be to use the Console. You substitute your scanner to read from a File returned from a new method:

Scanner input = new Scanner(getFileFromUser());
...

private static File getFileFromUser() {
   Console c = System.console();
   if (c == null) {
       System.err.println("No console.");
       System.exit(1);
   }
   String filePathname = c.readLine("Enter file pathname: ");

   return new File(filePathname);
}

Also don't forget to close your scanner in the end of main method to avoid resource leak:

input.close();
Boris
  • 22,667
  • 16
  • 50
  • 71