1

I am trying to understand why my code is not writing the output to the textfile as I expect it to work. My program takes a filename as a command line argument, and prints some text to the file as well as the screen. It is a bit more complicated since it uses classes and objects to demonstrate how objects work. Can anyone help decipher why it is not writing to the file? Here's my code:-

public class Mamoonp3test {

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

        //Create array of 10 guitar (Mamoonp3) objects
        final int NUMBER_OF_INSTANCES = 10;
        Mamoonp3[] objectNames = new Mamoonp3[NUMBER_OF_INSTANCES];

        try 
        {       
            String fileName = new String(args[0]);

            for(int i=0; i<NUMBER_OF_INSTANCES; i++) {
                objectNames[i] = new Mamoonp3(FileName);
                System.out.println("This is guitar number: " + i);
                objectNames[i].tuneGuitar();
                objectNames[i].playGuitar();
                objectNames[i].displayAcronym();
                objectNames[i].stopGuitar();
                System.out.println("---------------------------");
                }
        }
        catch (Exception e)
        {
            System.out.println("please provide an input file");
            System.out.println("Usage: java Mamoonp3test filename.txt");
        }
    }

}

import java.io.*;

public class Mamoonp3 {

    final int NUMBER_OF_STRINGS = 6;
    char[] stringNames = {'E','A','D','G','B','E'};
    int[] stringNumbers = {6,5,4,3,2,1};
    String[] stringPitch = {"Sixth","Fifth","Fourth","Third","Second","First"};

    boolean isTuned;
    boolean isPlaying;
    String stringAcronym = new String("Even After Dinner Giant Boys Eat");

    //create a PrintWriter for output
    PrintWriter output;

    public Mamoonp3(String fileName) throws Exception{
        isTuned = false;
        isPlaying = false;
        // create target file
        File targetFile = new File(fileName);
        //create a PrintWriter for output
        output = new PrintWriter(targetFile);
    }

    public void tuneGuitar() {
        System.out.println("The guitar is now tuned.");
        for (int i=0; i<NUMBER_OF_STRINGS; i++) {
            System.out.println(stringNames[i] + " is string number " + stringNumbers[i] + " and ranked " + stringPitch[i] + " in pitch");
            output.print(stringNames[i] + " is string number " + stringNumbers[i] + " and ranked " + stringPitch[i] + " in pitch");
            output.close();
        }
    }

    public void playGuitar() {
        System.out.println("The guitar is now playing.");
        output.print("The guitar is now playing.");
        output.close();
    }

    public void stopGuitar() {
        System.out.println("The guitar is now stoped.");
        output.print("The guitar is now stoped.");
        output.close();
    }

    public void displayAcronym() {
        System.out.println("Always remember your string names!");
        System.out.println("Heres a reminder: " + stringAcronym);
        output.print("Always remember your string names!");
        output.print("Heres a reminder: " + stringAcronym);
        output.close();
    }

}

1 Answer 1

2

You're setting the File of an object that you then do nothing with, that you're not writing with,

Mamoonp3 newObject = new Mamoonp3(fileName);

... and not setting the File in objects that you try to write with. Check which constructors you are using: every Manoop3 object created in the for loop. To see that this is so, check which constructors you're using

I suggest that you change your approach entirely.

  • Get all file input and output out of your Mamoonp3 class.
  • Instead, that class should concern itself with representing the state of the musical instrument, and nothing else.
  • Give the class a decent toString() override method.
  • I & O should go elsewhere in a separate class of its own.
  • Give your I&O class a method that allows you to pass Mamoonp3 objects into it so that they can be written.
  • As an aside, you almost never would use new String(anything). Just use args[0].
  • Always close your PrintWriter when you are done writing. This is likely causing your error.

Edit

Possibly another way to solve this:

  • Create a PrintWriter object in the main method.
  • Give your Manoop3 class a PrintWriter field and a constructor that takes this PrintWriter and sets its field with it.
  • Write with the PrintWriter in Manoop3, but don't close it.
  • Then close the PrintWriter in the main method when all Manoop3 objects have completed their use of it.
Sign up to request clarification or add additional context in comments.

10 Comments

@user76020: your latest code won't compile as Filename != fileName
Is there a quicker way, I have a deadline in 2 hours and I don't have time to redesign the whole thing...any further help is much appreciated...
@user76020: If you're going to close your output at the end of each method, you need to re-open it at the beginning of each method.
ooops, sorry that was a typo
@user76020: you must make sure that the output is closed when you are done with it. Consider either giving the class a closeOutput() method that you call at the end that does this, or open and close it in every method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.