Home > Back-end >  Why is my text file not being read and printed?
Why is my text file not being read and printed?

Time:10-02

My problem right now is that when I type in my filename in the console, it says that the file is not found. How can I fix that and it also prints whatever is in the file, into the console?

Below is my code for the display and reading the file.

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

public class MazeMaster {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter the maze's filename (no .txt): ");
      Maze m = new Maze(sc.next()   ".txt");
      // Maze m = new Maze();    //extension   
      System.out.println("Options: ");
      System.out.println("1: Mark all dots.");
      System.out.println("2: Mark all dots and display the number of recursive calls.");
      System.out.println("3: Mark only the correct path.");
      System.out.println("4: Mark only the correct path. If no path exists, say so.");
      System.out.println("5: Mark only the correct path and display the number of steps.\n\tIf no path exists, say so.");
      System.out.print("Please make a selection: ");
      m.solve(sc.nextInt());
      m.display();      //display solved maze
   } 
}

class Maze {
   //constants
   private final char WALL = 'W';
   private final char DOT = '.';
   private final char START = 'S';
   private final char EXIT = 'E';
   private final char TEMP = 'o';
   private final char PATH = '*';
   //instance fields
   private char[][] maze;
   private int startRow, startCol;
  
   //constructor
   public Maze() {
   
   }
    
    /* 
     * Copy Constructor  
     */
   public Maze(char[][] m) {
      maze = m;
      for(int r = 0; r < maze.length; r  ) {
         for(int c = 0; c < maze[0].length; c  ) { 
            //identify start location
            if(maze[r][c] == START) {
               startRow = r;
               startCol = c;
            }
         }
      }
   } 
    
    /* 
     * Use a try-catch block
     * Use next(), not nextLine()  
     */
   public Maze(String filename) {
      Scanner infile = null;
      try {
         infile = new Scanner(new File(filename   ".txt"));
      }
      catch(Exception e) {
         System.out.println("File not found");
         return;
      }
      
      //read the file
      maze = new char[infile.nextInt()][infile.nextInt()];
      for(int r = 0; r < maze.length; r  ) {
         char[] line = infile.next().toCharArray();
         for(int c = 0; c < maze[0].length; c  ) {
            maze[r][c] = line[c];
            if(maze[r][c] == START) {
               startRow = r;
               startCol = c;
            }
         }
      }
      
   }
   
   public char[][] getMaze() {
      return maze;
   }
   
   public void display() {
      if(maze == null) 
         return;
         
      for(int a = 0; a < maze.length; a  ) {
         for(int b = 0; b < maze[0].length; b  )
            System.out.print(maze[a][b]);
         System.out.println();
      }
      System.out.println();
   }
}

I don't know if this is needed but this is what the text files tend to look like with the number of columns and rows at the top of the file.

maze1.txt

8 8
WWWWWWWW
W....W.W
WW.WW..W
W....W.W
W.W.WW.E
S.W.WW.W
WW.....W
WWWWWWWW

Any help is much appreciated.

CodePudding user response:

The file name that you are passing in ends up at new File(filename ".txt"). Assuming that you are running this program from your IDE with default settings, and that you are passing in a valid file name, then that file name has to be in the root of your project directory. So if your project directory is MyProject and your text file is test.txt, then it should be placed under MyProject/test.txt.

Note that you can use the following code to see which file you are trying to read, and whether it exists or not:

File file = new File(filename   ".txt");
System.out.println("File path: "   file.getAbsolutePath()   "(exists: "   file.exists()   ")");

CodePudding user response:

This is due to you're trying add .txt extension multiple times. first when you pass it to constructor and second while making the File object which will end to adding multiple extension.

public class MazeMaster {
   public static void main(String[] args) {
      ....
      Maze m = new Maze(sc.next()); // we removed the extension cause is added by Maze class or you can do it vice versa
      ....
   } 
}

and as of now you are just taking file from root folder where your class exist so keep in mind that file should be in same folder as MazeMaster class.

  •  Tags:  
  • java
  • Related