Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,155 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,587 people online right now. Registration is fast and FREE... Join Now!




Writing data to a text file

 

Writing data to a text file, Need help possibly writing a loop in a text file

CherylP

24 Nov, 2008 - 11:16 AM
Post #1

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 38


My Contributions
Here is my code for this text file:

CODE

import java.util.Scanner;  // Needed for Scanner class
import java.io.*;          // Needed for file classes

System.out.println("Please enter the name of the file.");
filename = keyboard.nextLine;

Filewriter fwriter = new Filewriter(student.txt);
PrintWriter outputFile = new Printwriter(fwriter);
outputFile.open();

//Now that we're open, we can do what we want, right?



And...that's where my excitement ended. How do I create a loop that will display the contents of the file with each line numbe followed by a colon? Like...print out:

1:

...wait for the user to enter data, and then when they press enter, show a number two? I am just assuming this will be done with a loop...

Any help is appreciated, thanks in advance!

User is offlineProfile CardPM
+Quote Post


BigAnt

RE: Writing Data To A Text File

24 Nov, 2008 - 04:58 PM
Post #2

May Your Swords Stay Sharp
Group Icon

Joined: 16 Aug, 2008
Posts: 2,391



Thanked: 100 times
Dream Kudos: 75
My Contributions
Something like this should work:

int linenumber = 0;
do
-->print to console: ++linenumber + ": "
-->get input
-->print to file: linenumber + ": " + user input
while(continue condition)

Where at the end of while loop you ask the user if there is more input, if there is continue the loop.
User is offlineProfile CardPM
+Quote Post

sl4ck3r

RE: Writing Data To A Text File

24 Nov, 2008 - 05:14 PM
Post #3

D.I.C Regular
Group Icon

Joined: 22 Sep, 2007
Posts: 261



Thanked: 6 times
Dream Kudos: 25
My Contributions
this should help smile.gif

http://www.dreamincode.net/forums/showtopic63736.htm

User is offlineProfile CardPM
+Quote Post

CherylP

RE: Writing Data To A Text File

24 Nov, 2008 - 05:45 PM
Post #4

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 38


My Contributions
Thank you...much appreciated!
User is offlineProfile CardPM
+Quote Post

pbl

RE: Writing Data To A Text File

24 Nov, 2008 - 07:23 PM
Post #5

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
And

Filewriter fwriter = new Filewriter("student.txt");

student.txt between "" or you mean

Filewriter fwriter = new Filewriter(filename);


This post has been edited by pbl: 24 Nov, 2008 - 07:27 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:29PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month