Results 1 to 20 of 22
- 04-24-2011, 05:18 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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(); } } }
- 04-24-2011, 05:30 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-24-2011, 05:32 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Ya the problem is that my professor is making us use TextPad which as I am sure you know gives no help...
- 04-24-2011, 05:33 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Please remember that I am very new to this so any help with explanations would be great and thanks so much in advance...
- 04-24-2011, 05:42 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
- 04-24-2011, 05:46 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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.
- 04-24-2011, 05:48 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Your code just opens the streams and then gets the input. It never prints or writes the input to the file.
- 04-24-2011, 05:52 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
is that the out.write command?
- 04-24-2011, 06:19 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-24-2011, 08:42 AM #10
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
I think the best file reader is
FileInputStream in BufferedReader with StringTokenizer
- 04-24-2011, 08:48 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-24-2011, 08:50 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,379
- Blog Entries
- 7
- Rep Power
- 17
- 04-24-2011, 09:01 AM #13
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
- 04-24-2011, 09:03 AM #14
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
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
- 04-24-2011, 09:04 AM #15
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
- 04-24-2011, 09:19 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,379
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-24-2011, 09:22 AM #17
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
You don't need google? w3w
Google is your enemy? Monsterific
- 04-24-2011, 03:43 PM #18
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Thanks guys for all the help but I still cannot get it to work...would someone please show me the code please....
- 04-24-2011, 03:45 PM #19
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-24-2011, 03:49 PM #20
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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(); } } }
Similar Threads
-
How to write a program to calculate and display sum in two dimensional array?
By Javanoobs in forum New To JavaReplies: 12Last Post: 02-08-2011, 02:58 PM -
How to display queries results from Oracle according to user input?
By NeVerEveR in forum JDBCReplies: 1Last Post: 10-18-2010, 07:14 AM -
having a JSP display the results in a Portlet setup
By Drun in forum Web FrameworksReplies: 2Last Post: 01-27-2010, 03:36 PM -
JUnit is working properly, but takes forever to display results
By mnk007 in forum EclipseReplies: 0Last Post: 04-21-2009, 01:32 AM -
Read-File Write Display substring
By hiklior in forum New To JavaReplies: 3Last Post: 04-18-2008, 11:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks