Results 1 to 20 of 20
  1. #1
    turtles is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Unhappy Input/output to a text file through a GUI

    Hello!

    I have an issue with a project I have to do. I need to develop a Java program able to display some information found in a .txt file through a GUI in table form, and also allow the user to search for specific information in that database (the text file), like table row number (so only the row the user picks is displayed, not the entire database). Also, within the same GUI, the user has to be able to input new information which needs to be appended to the text file/database. So I need to read from and write to a text file through a GUI. I can use a maximum of 5 classes for this and I am struggling with it, if anyone could help. Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Input/output to a text file through a GUI

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: Input/output to a text file through a GUI

    Few points that sparked my interest:
    - A maximum of 5 classes? I can cram the whole lot into a single class if I need to, or I could do it in 25. Who has set the 5 class restriction and why? Even if it was your professor, he talks out of his ass. Giving out an assignment with the maximum number of classes is stupid. The number of classes depend on your programming style and abilities. It is *not* an indicator of quality.
    - Reading and writing to/from a textfile does not have anything to do with a GUI. Those are separated responsibilities. Get the reading/writing sorted out first, then build a GUI around that.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  4. #4
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Input/output to a text file through a GUI

    Spoon feed to the rescue.

    Reading:
    Java Code:
    ArrayList<String> lines = new ArrayList<>(); //A place to store the lines from the text file (so we can read from it later)
    File file = new File("path/to/file.txt"); // The text file
    BufferedReader br = new BufferedReader(new FileReader(file)); //A reader to allow reading from said text file 
    String line; //dat String doe
    while ((line = br.readLine()) != null) { //If there is something in the line of the text file
    lines.add(line); //add it to the storage of Strings (lines)
    }
    br.close(); //Close the stream
    Writing:
    Java Code:
    File file = new File("path/to/file.txt"); // The text file
    PrintWriter pw = new PrintWriter(file); //PrintWriter, its self explanatory
    pw.println("Hello"); //prints a new line of 'Hello' to the file.
    pw.flush(); //Make sure you call this whenever you write to the file
    pw.close(); //Make sure you call this when you're done with the PrintWriter
    Keep in mind my code throws an IOException. As it should
    Last edited by zFollette; 12-23-2013 at 09:28 PM.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: Input/output to a text file through a GUI

    Yeah you see spoonfed code is like a tutorial - it only shows you where to click, it does not tell you WHY you click there. And the WHY is an important key factor in understanding, that thing which people should leave with after asking for help.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Input/output to a text file through a GUI

    Quote Originally Posted by gimbal2 View Post
    Yeah you see spoonfed code is like a tutorial - it only shows you where to click, it does not tell you WHY you click there. And the WHY is an important key factor in understanding, that thing which people should leave with after asking for help.
    My comments are not completely worthless, and give OP something to go off of. See, OP should read my code and think - "Ok, I clearly need to specify a file for some kind of reader to look for, and if the line I am reading has stuff in it, I should record that. If not, move on. Then when I want to, I should tell my reader to stop reading." Its a simple process. People learn differently.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: Input/output to a text file through a GUI

    People also make the same mistakes over and over and try to validate their mistakes using the same arguments over and over. people learn differently that much is true - some do it very wrong, some do it very well. The same for how people instruct others.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Input/output to a text file through a GUI

    Quote Originally Posted by gimbal2 View Post
    People also make the same mistakes over and over and try to validate their mistakes using the same arguments over and over. people learn differently that much is true - some do it very wrong, some do it very well. The same for how people instruct others.
    "You're a crappy instructor" would suffice.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: Input/output to a text file through a GUI

    Nope; only that what you did in this thread was give crappy instructions - but with good intentions. But that's just my opinion, you make do with it what you want. Just be aware that if you continue to do it, other people will tell you the same but then less poorly worded than I did. And sorry for that, I tend to fail miserably trying to get a point across in a neutral manner :/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Input/output to a text file through a GUI

    Quote Originally Posted by gimbal2 View Post
    Nope; only that what you did in this thread was give crappy instructions - but with good intentions. But that's just my opinion, you make do with it what you want. Just be aware that if you continue to do it, other people will tell you the same but then less poorly worded than I did.
    Thanks, I'll keep that in mind. On a side note, you have impeccable grammar.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: Input/output to a text file through a GUI

    Quote Originally Posted by zFollette View Post
    pw.flush(); //Make sure you call this whenever you write to the file
    pw.close(); //Make sure you call this when you're done with the PrintWriter
    Well, A couple of things here.
    1. Calling flush after every write sort of defeats the purpose of a buffered (implied) output class.
    2. But if you must, PrintWriter also caters to that with autoflush (check out the API).
    3. Closing an output stream or writer usually flushes the buffer anyway (should be documented in the API).
    4. Since PrintWriter implements AutoCloseable, Closing is not necessary if a try with resource is used.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Input/output to a text file through a GUI

    AutoCloseable, Closing is not necessary
    I don't know why anyone promotes such shortcuts. It's a way add program bugs for the times that AutoCloseable is not implemented. Always closing an I/O class does not cost much and makes sure that it is done.
    The programmer has to remember: Is this the time I can skip it and is this the time I need it.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Input/output to a text file through a GUI

    Quote Originally Posted by jim829 View Post
    Well, A couple of things here.
    1. Calling flush after every write sort of defeats the purpose of a buffered (implied) output class.
    2. But if you must, PrintWriter also caters to that with autoflush (check out the API).
    3. Closing an output stream or writer usually flushes the buffer anyway (should be documented in the API).
    4. Since PrintWriter implements AutoCloseable, Closing is not necessary if a try with resource is used.

    Regards,
    Jim
    I see your point, however, say OP was printing to something with a live output, something the user needs to see, then flush is necessary. Just good to put it in there until you know exactly when/when not to use it. As for close(); I think its best that it stays in there.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: Input/output to a text file through a GUI

    Quote Originally Posted by Norm View Post
    I don't know why anyone promotes such shortcuts. It's a way add program bugs for the times that AutoCloseable is not implemented.
    Arguably, it prevents bugs, doesn't add them. The compiler will flag the try if AutoCloseable is not implemented. And based on published documentation, new features tend to be added very judiciously to the Java API so I assume there was sufficient cause. And the Java tutorials we all promote recommends it. But then, some folks feel the same way about generics. In any event I was just pointing it out to the responder.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Input/output to a text file through a GUI

    new features tend to be added very judiciously
    Sometimes I wonder. For example autoboxing:
    Java Code:
    anArrayList.add(123);       //  Adds an Integer object
    anArrayList.remove(123);  //  remove the 123 that was just added???
    Sorry, not really related and not much can be done. It's just another case that raises the bar for being a successful programmer. Many exceptions to remember.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    turtles is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: Input/output to a text file through a GUI

    I need 5 classes or less so the code looks tidy and and it's easy to read: a Main class, one that deals with the GUI, one containing the getters/setters, and one dealing with the input/output to and from the text file, which can be split into two if it gets too crowded. It's writing to a text file that I'm struggling with, everything I tried so far hasn't worked.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: Input/output to a text file through a GUI

    new features tend to be added very judiciously
    Quote Originally Posted by Norm View Post
    Sometimes I wonder. For example autoboxing:
    Java Code:
    anArrayList.add(123);       //  Adds an Integer object
    anArrayList.remove(123);  //  remove the 123 that was just added???
    Sorry, not really related and not much can be done. It's just another case that raises the bar for being a successful programmer. Many exceptions to remember.
    Yeah. And upon further reflection the notion that features are added "judiciously" must be taken in context. It's from the designers. What else are they going to say :).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    turtles is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: Input/output to a text file through a GUI

    Thank you everyone for your posts! :)

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Input/output to a text file through a GUI

    everything I tried so far hasn't worked.
    Can you explain what happens when the code is compiled and executed? Copy and post any error messages.
    And post the code that you are having problems with.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Input/output to a text file through a GUI

    Post your current writing method.

Similar Threads

  1. Puzzled about System Input / Output to a file.
    By CodeMonkeyAlx in forum New To Java
    Replies: 4
    Last Post: 03-28-2013, 07:44 PM
  2. Trouble with J File Chooser for input and output
    By prabhdsun in forum Advanced Java
    Replies: 1
    Last Post: 12-04-2012, 06:03 AM
  3. Input/ output file
    By 3MAD in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 12:59 PM
  4. File input output
    By edcaru in forum New To Java
    Replies: 5
    Last Post: 12-19-2010, 05:52 PM
  5. Replies: 8
    Last Post: 05-12-2010, 07:19 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
  •