Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    sandbudd is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default write to file and display results

    Hey Guys I am a new to java and need to write a users input to a file and display the information. I can get it to display but cannot get it to write to the file. I am sure this is something simple if I could please have some help. If I take this out it shows the info
    try{
    // Create file
    FileWriter fstream = new FileWriter("out.txt");
    BufferedWriter out = new BufferedWriter(fstream);



    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class StringVariables {
    
    public static void main(String[] args) {
    
    try{
        // Create file
        FileWriter fstream = new FileWriter("out.txt");
            BufferedWriter out = new BufferedWriter(fstream);
    
    Scanner user_input = new Scanner (System.in);
    
    String userId;
    System.out.print("Enter Product ID: ");
    userId = user_input.next();
    
    String productName;
    System.out.print("Enter product name: ");
    productName  = user_input.next();
    
    String productPrice;
    System.out.print("Enter product price: ");
    productPrice  = user_input.next();
    
    }
    
    }
    
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    First, I suggest you use readLine, read simply grabs everything in the first word(stops at first space character).

    The problem is your code just gets input and stores it, it never displays it or write to the file.

    You also should consider to catching exceptions.

  3. #3
    sandbudd is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Ya the problem is that my professor is making us use TextPad which as I am sure you know gives no help...

  4. #4
    sandbudd is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Please remember that I am very new to this so any help with explanations would be great and thanks so much in advance...

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Using text pad isn't too bad, you will learn well like that. Does it have syntax highlighting?

    When you use methods or classes that can throw exceptions you wrap them in try catch clauses.

    You try to do something, and you are ready to catch and handle the exceptional situations that may occur.

    So when you open streams you want to be ready to catch an IOException. Google the oracle tutorials for exceptions and file writing.

    You want to prompt the user for input and store it in the string variables with a scanner like you did. Then you can use the variables to display the info, and finally write to file.

    Pseudo code
    Java Code:
    declare scanner initialize scanner
    prompt for input
    store input in variables
    print variables
    try
      open streams
      write to file
    end try
    catch
      catch pertinent exceptions
    end catch
    finally
      try
        close streams
      end try
      catch
        catch exceptions
      end catch
    end finally

  6. #6
    sandbudd is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    sunde887 isn't that what I have done? I am not asking you to do the work for me but if you could give me an example it would be greatly appreciated as I have to turn this in tomorrow with it functioning.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your code just opens the streams and then gets the input. It never prints or writes the input to the file.

  8. #8
    sandbudd is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    is that the out.write command?

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Perhaps, the best way to learn this is to try it out. The io tutorials also give fairly good examples of writing to a file.

  10. #10
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    I think the best file reader is
    FileInputStream in BufferedReader with StringTokenizer

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    He is looking to write not read, and shy use string tokenizer? According to the API, it's a legacy class and it's use is discouraged.

    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

    That is from the API.

    Also, I believe buffered reader or print writer are preferred(not 100% on this)

    Hopefully I am not responding to one of those spammers with vague semi comparable statements.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by RichersooN View Post
    I think the best file reader is
    FileInputStream in BufferedReader with StringTokenizer
    Care to elaborate on that non sequitur statement?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    He is looking to write not read, and shy use string tokenizer? According to the API, it's a legacy class and it's use is discouraged.

    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

    That is from the API.

    Also, I believe buffered reader or print writer are preferred(not 100% on this)

    Hopefully I am not responding to one of those spammers with vague semi comparable statements.
    I thought he also look for read file, cause the title is "write to file and display results"

    I don't know whats the history of StringTokenizer but its work perfectly fine to my Java Projects with Textfiles as database

  14. #14
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    The best file writer is
    FileWriter in a BufferedWriter
    Cause you can choose whether you append data to a textfile or you rewrite the whole data of textfile

  15. #15
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Care to elaborate on that non sequitur statement?

    kind regards,

    Jos
    Its with him to google the API for that

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by RichersooN View Post
    Its with him to google the API for that
    That would be fun: we can all reply our "claims out of the blue" and tell the OP to google for the validity of our claims. OK, here goes: the best class for data input is the InputStream; it gives you byte level control, something other classes don't offer and you can do with those bytes whatever you want. Google is your friend.

    kind reards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    You don't need google? w3w
    Google is your enemy? Monsterific

  18. #18
    sandbudd is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks guys for all the help but I still cannot get it to work...would someone please show me the code please....

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    We don't do that here. I know how frustrating it can get to be stuck, but getting the code doesn't help you learn. Show us your latest attempts and say what you are stuck on.

  20. #20
    sandbudd is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    This is where I am at...I know that I am missing something....I can get it to display but not write to a file?

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class StringVariables {
    
    public static void main(String[] args) {
    
    try{
        // Create file
        FileWriter fstream = new FileWriter("out.txt");
            BufferedWriter out = new BufferedWriter(fstream);
    
    Scanner user_input = new Scanner (System.in);
    
    String userId;
    System.out.print("Enter Product ID: ");
    userId = user_input.next();
    
    String productName;
    System.out.print("Enter product name: ");
    productName  = user_input.next();
    
    String productPrice;
    System.out.print("Enter product price: ");
    productPrice  = user_input.next();
    
    }
    
    }
    
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 12
    Last Post: 02-08-2011, 02:58 PM
  2. Replies: 1
    Last Post: 10-18-2010, 07:14 AM
  3. having a JSP display the results in a Portlet setup
    By Drun in forum Web Frameworks
    Replies: 2
    Last Post: 01-27-2010, 03:36 PM
  4. Replies: 0
    Last Post: 04-21-2009, 01:32 AM
  5. Read-File Write Display substring
    By hiklior in forum New To Java
    Replies: 3
    Last Post: 04-18-2008, 11:45 AM

Posting Permissions

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