0

I am trying to prompt the user to enter a file name and search for the filename and save it to 2 2-D array.

Example of the file is:

BBBBB
BBBBB
BBBBB
BBBBB

public class maze_2D{
static Scanner s = new Scanner(System.in);

public static void FromFile() throws Exception{//
  System.out.println("Enter File name");
   String file = s.nextLine();
   File f = new java.io.File(file);
   Scanner scanner = new Scanner(f);
   // Read from file.....

But when I run the program, i get an error

Enter Filename
java.io.FileNotFoundException: 

Why is this happening, why this scanner doesn't allow me to enter any file name?

Secret.BoBo
  • 25
  • 1
  • 6

2 Answers2

0

While inputting file name in command prompt give the full path including file name with extension where your file resides in the File System.

System.out.println("Enter File name");
   String file = s.nextLine();
   File f = new java.io.File(file);

    try {

        Scanner sc = new Scanner(f);

        while (sc.hasNextLine()) {
            int i = sc.nextInt();
            System.out.println(i);
        }
        sc.close();
    } 
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
 }
  • I don't think you read the question carefully - it's not even letting him enter the filename. – D M Apr 17 '17 at 20:09
0

I made a little class using most of your code ad it worked fine... try examining your path+filename to ensure it is really there.

I have heard of scanner.getInteger forcing you to add a scanner.nextLine() after it but you are using nextLine to the the fileName so this shouldn't be the case.

public class NewClass {

    static Scanner s = new Scanner(System.in);

    public static void main(String args[]) throws Exception {
        FromFile();
    }

    public static void FromFile() throws Exception {
        System.out.println("Enter File name");
        // I enter '/Users/myMame/Downloads/testFile.txt'
        String file = s.nextLine();

        File f = new java.io.File(file);
        Scanner scanner = new Scanner(f);

        // do your 2D array manipulation 

        while(scanner.hasNextLine()){
            String line = scanner.nextLine();
            System.out.println("line: " + line);
        }

    }

}
Matt
  • 326
  • 1
  • 10