Question

Let me preface this by saying that I'm extremely new to java. This is my eighth week in the class and I'm stuck on a project. Here is what I have so far:

import java.io.*;


public class Guitar {
// Initialize variables 
boolean isPlaying;
boolean inTune;
char[] guitStrings = {'D', 'G', 'C', 'A'}; // Guitar strings
int numOfStrings = 4; // Number of strings the guitar has.

public void Guitar(){
isPlaying = false; // Guitar is not playing by default.
inTune = false; // Guitar is not tuned by default.
System.out.println("The guitar is not tuned and is not playing.");
}

public void isPlaying(){
    System.out.println("Your guitar is now playing!");
    isPlaying = true; // Set guitar to playing
}
public void inTune(){
    System.out.println("Your guitar is now tuned!");
    inTune = true; // Set guitar to tuned.
}

public void stopPlaying(){
    isPlaying = false; // Set isPlaying to false.
    System.out.println("Your guitar has finished playing!");
}
public void notes(){
    System.out.println("The guitar has played a total of " + numOfStrings + 
            " strings and they are: " + guitStrings[0] + "," + guitStrings[1] + "," 
            + guitStrings[2] + "," + guitStrings[3]);
    }

public static void main(String[] args) throws IOException{
    java.io.File file = new java.io.File("guitartest.txt");
    if(file.exists()){
        System.out.println("File already exists!");
        System.exit(1);
    }

    // Create a file
    java.io.PrintWriter output = new java.io.PrintWriter(file);

    Guitar[] guit = new Guitar[10];   
    for (int i = 0; i < guit.length; i++){
        guit[i] = new Guitar();
        guit[i].Guitar();
        guit[i].inTune();
        guit[i].isPlaying();
        guit[i].notes();
        guit[i].stopPlaying();
    }
}    
}

This program does everything I need it to do, but we have one last step on the project. I must output this to a text file from the command line. I've changed the last bit of code to this:

public static void main(String[] args) throws IOException{
    java.io.File file = new java.io.File("guitartest.txt");
    if (file.exists()){
        System.out.println("This file already exists!");
        System.exit(1);
    }

    // Create a file
    java.io.PrintWriter output = new java.io.PrintWriter(file);

    Guitar[] guit = new Guitar[10];   // Create 10 instruments
    for (int i = 0; i < guit.length; i++){
        output.println(guit[i] = new Guitar());
        output.println(guit[i].Guitar());
        output.println(guit[i].inTune());
        output.println(guit[i].isPlaying());
        output.println(guit[i].notes());
        output.println(guit[i].stopPlaying());
    }
}

This compiles the codes, and displays the results I want in the console, but the text file guitartest.txt is completely blank. I am NOT looking for someone to complete this assignment for me, I'm just looking for any advice or resources you could point me to. Thank you very much!

Was it helpful?

Solution

PrintWriter is buffered, that means that the text you're writing to it is stored in its internal buffer before being actually written to the file. So you need to call a close() method when you're done writing, so that the PrintWriter object wrote the data to the file and closed it.

You can also call flush(), this may be useful if you want your data written now but also want to continue using the PrintWriter object.

OTHER TIPS

After you're done writing

output.flush();
output.close();

The PrintWriter constructor that accepts a File is implemented as

public PrintWriter(File file) throws FileNotFoundException {
    this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
         false);
}

In other words, your outputs are being buffered and need to be flushed from time to time or all in one shot.

Call

output.flush();

or

output.close();

when you are finished using it.

You probably need to flush the output stream at the end, i.e. after the for loop in the main() method do

output.flush();
output.close();

You can include both of these calls in a try/catch block, for safety:

try {
    output.flush();
    output.close();
} catch (Exception e) {
    e.printStackTrace();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top