Results 1 to 12 of 12
  1. #1
    alex.start is offline Member
    Join Date
    Nov 2012
    Location
    Australia
    Posts
    9
    Rep Power
    0

    Default Output to file not writing out desired string

    Using JOptionPane input windows to gain 3 strings of information from the user. After all 3 are complete, combine and store as string separated by lines and display confirm window. If OK is pressed, write out to file.

    I have 3 classes

    Input: Displays JOptionPane which prompt the user to enter values.
    Output: Has methods which write out to a specified file and write out the desired text using FileWriter and PrintWriter objects.
    Run: Holds the main() method which creates objects of both classes and calls the required methods.

    Input:
    import javax.swing.*;
    public class Input
    {
    private String firstName = "";
    private String lastName = "";
    private int currentAge = 0;
    private char gender = 0;
    private String allDetails = "";

    public void inputDetails()
    {
    firstName = JOptionPane.showInputDialog(null, "Enter your first name:");
    lastName = JOptionPane.showInputDialog(null, "Enter your last name:");
    currentAge = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your age:"));
    }

    public void combineDetails()
    {
    allDetails = "First name: " + firstName + "\nLast name: " + lastName + "\nAge: " + currentAge;
    }

    public void displayDetails()
    {
    JOptionPane.showConfirmDialog(null, allDetails);//display details on separate lines
    }

    public String getDetails()
    {
    return allDetails;
    }
    }

    Output:
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    public class Output
    {
    Input input = new Input();
    String path;
    boolean appendToFile = false;

    public void writeFile(String filePath)//writes to a file location
    {
    path = filePath;
    }

    public void writeToFile(String textLine) throws IOException//Writes text to file
    {
    FileWriter write = new FileWriter(path);
    try (PrintWriter printLine = new PrintWriter(write)) {
    printLine.printf("%s", textLine);
    }
    }
    }

    Run:
    import java.io.IOException;
    public class Run
    {
    public static void main(String [] args) throws IOException
    {
    Input inputClass = new Input();
    Output outputClass = new Output();
    try{
    inputClass.inputDetails();
    }
    catch(NumberFormatException error){
    System.out.printf(error.getMessage());
    }
    inputClass.combineDetails();
    inputClass.displayDetails();
    try{
    outputClass.writeFile("Identity.txt");
    outputClass.writeToFile(inputClass.getDetails());
    }
    catch(IOException e){
    System.out.printf("%s", e.getMessage());
    }
    }
    }

    This is the text in the file: First name: StuffgoeshereLast name: HereaswellAge: HereToo

    I intended this to display:
    First name: Stuff
    Last name: Goes
    Age: Here

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,079
    Rep Power
    6

    Default Re: Output to file not writing out desired string

    What's your question?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    alex.start is offline Member
    Join Date
    Nov 2012
    Location
    Australia
    Posts
    9
    Rep Power
    0

    Default Re: Output to file not writing out desired string

    This is the text in the file: First name: StuffgoeshereLast name: HereaswellAge: HereToo

    I intended this to display:
    First name: Stuff
    Last name: Goes
    Age: Here

    How do I get my desired output?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,079
    Rep Power
    6

    Default Re: Output to file not writing out desired string

    You write the code to do so. And that is something you have to do, possibly with a good amount of research and experimentation to preceed it. What are you stuck on exactly?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: Output to file not writing out desired string

    First name: StuffgoeshereLast name: HereaswellAge: HereToo

    Well, your OutputFile code is not putting any carriage returns in there.
    Which raises the question of why you are using printf (which is all about formatting output) for a single string?
    Why not simply use println?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    alex.start is offline Member
    Join Date
    Nov 2012
    Location
    Australia
    Posts
    9
    Rep Power
    0

    Default Re: Output to file not writing out desired string

    And that is what I have attempted to do. As you can see my code is printed above. The program does write out to a file, except I am getting it all printed onto one line. I want it in separate lines and I have made a method which combined all strings and formatted into separate lines and tried to use that method to write it out. But what I get is my 3 strings in one line in my txt file.

  7. #7
    alex.start is offline Member
    Join Date
    Nov 2012
    Location
    Australia
    Posts
    9
    Rep Power
    0

    Default Re: Output to file not writing out desired string

    I don't know much about carriage returns...any tips on that?

    I was looking off an app whilst working remotely and in this example for writing to text files, they had used printf. Plus I kinda like printf more, even if I don't require any major formatting. It seems to easy to just use println right now

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: Output to file not writing out desired string

    Well...use println.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    alex.start is offline Member
    Join Date
    Nov 2012
    Location
    Australia
    Posts
    9
    Rep Power
    0

    Default Re: Output to file not writing out desired string

    No no sorry that sentence doesn't sound right. I mean if I had a choice I would use printf. I have tried both methods for my code and I get the same result. :(

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Output to file not writing out desired string

    Quote Originally Posted by alex.start View Post
    I get the same result.
    Since printf method does not include CRLF and println does, I find that statement hard to believe.

  11. #11
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Output to file not writing out desired string

    Have you tried this out yet?

    Java Code:
    allDetails = "First name: " + firstName + "\r\nLast name: " + lastName + "\r\nAge: " + currentAge;

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Output to file not writing out desired string

    1. That involves a lot of String concatenation. Using several print statements would probably be more efficient.
    2. \r\n is not platform independent. If you don't use println method then use System.getProperty instead.

Similar Threads

  1. Writing output to a file
    By Ria in forum New To Java
    Replies: 1
    Last Post: 09-26-2012, 11:12 AM
  2. Replies: 1
    Last Post: 02-13-2012, 02:51 PM
  3. Writing output file
    By doymand in forum New To Java
    Replies: 2
    Last Post: 11-05-2010, 01:24 PM
  4. Writing a block of text as it is to output file
    By bikashg in forum New To Java
    Replies: 5
    Last Post: 05-29-2010, 11:38 PM
  5. Help in writing the output in html file
    By aks.nitw in forum Advanced Java
    Replies: 5
    Last Post: 01-18-2010, 01:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •