Note that there are some explanatory texts on larger screens.

plurals
  1. POCan not get java to write to a text file using PrintWriter
    text
    copied!<p>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:</p> <pre><code>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 &lt; guit.length; i++){ guit[i] = new Guitar(); guit[i].Guitar(); guit[i].inTune(); guit[i].isPlaying(); guit[i].notes(); guit[i].stopPlaying(); } } } </code></pre> <p>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: </p> <pre><code>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 &lt; 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()); } } </code></pre> <p>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!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload