-1

I need to make a movie ticket program. I've written the majority of the class. The last thing to do is create a text file with PrintWriter by sending the info to the text file.

I haven't gotten started on the second part yet. For now I'm just trying to get the first two lines of the seating chart to print. This doesn't require info from the class.

The problem is that nothing is being added to the text file at all. This is what I have so far:

public void createSeatingChart() throws FileNotFoundException 
{
    File seatingChart = new File("C:/Users/Chris/Documents/NetBeansProjects/Auditorium/seatingChart.txt");
    PrintWriter pw = new PrintWriter(seatingChart);

    pw.println("\t\tSeats");
    pw.println("\t123456789012345678901234567890");

    pw.close();
}

I'm pretty sure it's something small that I'm missing but I'm not sure what it is. Any ideas?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • How do you know something is wrong? Your question is missing important contextual information: what errors are you seeing? what misbehaviors? How are you calling this? Are you handling exceptions appropriately? – Hovercraft Full Of Eels Mar 29 '17 at 00:58
  • no errors at all only exceptions i was getting was filenotfound which i threw – Christopher Collar Mar 29 '17 at 00:59
  • 1
    Debugging 101: Either put a breakpoint or a `System.out.println` call at the start of this function and make sure it's being called. Then single-step (or add more `println`s) to see what actually happens. The *first* subject any course should cover is debugging. – paxdiablo Mar 29 '17 at 01:00
  • 1
    I think you have the file path wrong. And, good thing too. The [`PrintWriter(File)`](https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#PrintWriter(java.io.File)) javadoc notes *The file to use as the destination of this writer. If the file exists then it will be truncated to zero size*. – Elliott Frisch Mar 29 '17 at 01:15
  • Seriously, use punctuation. You will find that it helps with the clarity of your own thoughts to slow down sometimes. – Mad Physicist Mar 31 '17 at 15:38

1 Answers1

0

Here is a Simple Solution That Worked For me Without any Errors.

As Declared in the JavaDoc for the PrintWriter Class:

FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file

I Think The Problem of the FileNotFoundException is because you are trying to Create a File inside a directory not existed.

So to Solve the problem you have to check firstly the Parent Directory of the file you want to create is it already existed or not, if it's not exist you have to create it first then you can try to write to the file using the PrintWriter

Note: the Second if Statement is optionnelle You can Remove it because the PrintWriter will create the file if not exist (Just for Demonstration Purpose).

public void createSeatingChart() throws IOException 
    {

        File seatingChart = new File("C:/Users/Chris/Documents/NetBeansProjects/Auditorium/seatingChart.txt");

        // Check The parent Directory is Exist or Create it.
        if (!seatingChart.getParentFile().exists()) seatingChart.getParentFile().mkdirs();

        // create a new file if not exist 
        if (!seatingChart.exists()) seatingChart.createNewFile();


        PrintWriter pw = new PrintWriter(seatingChart);

        pw.println("\t\tSeats");
        pw.println("\t123456789012345678901234567890");

        pw.close();


    }
Y. Afnisse
  • 150
  • 2
  • 15