Results 1 to 4 of 4
Thread: Using Printwriter (java.io.*)
- 12-24-2008, 06:03 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 7
- Rep Power
- 0
Using Printwriter (java.io.*)
Hi' all,
I have a problem trying to get my program to read names from the user and store them in a file. I did some debugging and managed to narrow it down to my use of the pwStream variable, would appreciate any help on this:
CODE:
/** Purpose: To learn how to use the java io package and incorporate exception handling in the program. Specifically, I am trying to get my program to request the user for 3 names, and store the names in a file called 'NAMES.txt'.
*/
import java.util.*;
import java.io.*;
public class Test
{
public static void main(String [] args)
{
try
{
PrintWriter pwStream= new PrintWriter(new BufferedWriter (new FileWriter ("NAMES.txt")));
Scanner sc=new Scanner(System.in);
String str=null;
System.out.println("Please enter 3 names to be stored in the file: ");
for (int i=0;i<3;i++)
{
str=sc.next();
//System.out.println(str);
pwStream.println(str); // here is where my problem is, after execution, the file NAMES.txt contains no names
}
}
catch (IOException e)
{
System.out.println("Error!" );
}
}
}
END OF CODE
Thanks for helping :)
p.s. sorry bout the indents, i cant get my program to be indented when it is published here, got to figure that one out.Last edited by LifeWithJava; 12-24-2008 at 06:07 AM.
- 12-24-2008, 06:51 AM #2
cant get my program to be indented when it is published hereJava Code:for (int i=0;i<3;i++) { System.out.println("enter name " + (i+1)); str=sc.nextLine(); //System.out.println(str);
[code]
// paste code here
[/code]
- 12-24-2008, 07:56 AM #3
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
you need to Flush the stream after all your write method call
- 12-25-2008, 08:07 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 7
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks