Results 1 to 6 of 6
- 12-05-2010, 01:35 PM #1
Member
- Join Date
- Nov 2010
- Location
- Beirut, Lebanon
- Posts
- 36
- Rep Power
- 0
simple file output question thanks in advance
ok so i wrote a program that reads from a file and outputs to a file. thing is the input file has several numbers and the output file is just printing the last answer(overwriting the others)
the program takes a number ex"683" finds the largest and smallest digit in the number then calculates its range
below is the part that prints to the output
public static void main(String args[]) throws FileNotFoundException
{
Scanner input = new Scanner (new File (args[0]));
while(input.hasNextInt())
{
int num = input.nextInt();
int range = digitRange(num);
PrintStream output = new PrintStream(new File("digit-range.out"));
output.print("The digit range of" + "\"" + num + "\"" + " is " + range);
}
}
any suggestions?
-
Are you sure that you want to re-create the PrintStream object "output" inside the while loop? This will surely over-write anything that was written there before from the previous iterations of the loop. Why not create this object once, before the while loop, print to it inside the while loop, and close it after the while loop? Just a thought.
- 12-05-2010, 01:42 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
-
I don't think that this is going to help the OP and you may want to re-read the original question. I don't read it as his desiring to append to an existing file, but rather of a problem where he only writes the last bit of data to the file rather than all the data that was analyzed in previous iterations of the while loop.
- 12-05-2010, 02:11 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 12-05-2010, 03:34 PM #6
Member
- Join Date
- Nov 2010
- Location
- Beirut, Lebanon
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
question. thanks in advance for your help
By farahm in forum New To JavaReplies: 4Last Post: 11-22-2010, 11:25 AM -
simple question. thanks in advance fro your help
By farahm in forum Advanced JavaReplies: 3Last Post: 11-22-2010, 10:58 AM -
Simple question Hopefully, Scanner/file
By drucey in forum New To JavaReplies: 23Last Post: 10-25-2010, 01:55 PM -
java question output file
By dazednconfused in forum New To JavaReplies: 3Last Post: 09-17-2009, 03:07 PM -
Question about File renameTo() method : Abnormal output
By suvojit168 in forum New To JavaReplies: 1Last Post: 07-25-2009, 08:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks