Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Print to txt file

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Print to txt file

    My current code will generate a txt file that includes "Instrument [i] : " and then the boolean values (true or false) of the four methods that I've created. However, I want the text file to include output such as this here. Any help is greatly appreciated.

    cello1 is tuned. cello1.tuneInstrument = (boolean value)
    cello1 is playing. cello1.playInstrument = (boolean value)
    cello1 is not playing. cello1.stopPlaying = (boolean value)
    cello1 is ready for concert. cello1.readyConcert = (boolean value)

    /*
    *
    */
    package instruments;

    import java.io.File;
    import java.io.FileNotFoundException;

    public class TestInstruments {
    public static void main(String[] args) throws FileNotFoundException{

    Instruments cello = new Instruments();
    File file = new File("AshleyMasonp3test.txt");

    if (file.exists()){
    System.out.println("File already exists");
    System.exit(0);
    }

    try (java.io.PrintWriter output = new java.io.PrintWriter(file)) {

    int i = 1;
    while (i < 11) {
    System.out.println("Instrument " + i + ":");
    output.println("Instrument " + i + ":");
    output.println(cello.tuneInstrument());
    output.println(cello.playInstrument());
    output.println(cello.stopPlaying());
    output.println(cello.readyConcert());
    i++;
    }
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Print to txt file

    Text files contain Strings. What problems are you having buidling the Strings you want to write to the text file?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Print to txt file

    So with the while loop, instead of printing, cello1 is tuned. cello1.tuneInstrument = true, the text file will have only the value of true.

    int i = 1; 
    while (i < 11) {
    System.out.println("Instrument " + i + ":");
    output.println("Instrument " + i + ":");
    output.println(cello.tuneInstrument());
    output.println(cello.playInstrument());
    output.println(cello.stopPlaying());
    output.println(cello.readyConcert());
    i++;
    }
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Print to txt file

    The code should build the String you want written to the file using the values returned from the method.
    If the method returns true, what String do you want written?
    If the method returns false, what String do you want?

    As you build the message try to use the common parts in one spot and use an if statement to separate the different parts. For example:
    the cello is tuned
    the cello is not tuned
    The colored parts are the common parts. The differences are an empty String in the first line and " not" in the second line.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Print to txt file

    Should that if statement be included in the while loop? The reason I'm asking is the tuneInstrument has to be a boolean field. Thus, I have this code for that method.

     public boolean tuneInstrument(){
         System.out.println("The instrument is tuned.");
         return isTuned = true;
     }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Print to txt file

    Where is the code that builds the String to be written to the file? That would be where you would test values to see how to build the String.
    For my previous example:
    if true don't add a String
    if false add in the String: "not "
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: November 13th, 2012, 07:02 AM
  2. Operation on a txt file..
    By Davidifier in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2012, 01:30 PM
  3. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  4. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  5. How to save statistics of a game in a binary file instead of txt file?
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 19th, 2009, 05:05 AM