Results 1 to 6 of 6
  1. #1
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

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

    Default

    Quote Originally Posted by farahm View Post
    any suggestions?
    Yep: wrap your PrintStream around a FileOutputStream and make that latter stream append its stream contents (see the constructor for that class).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    Yep: wrap your PrintStream around a FileOutputStream and make that latter stream append its stream contents (see the constructor for that class).

    kind regards,

    Jos
    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.

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

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    Ah, ok, I obviously don't understand the OP's problem so I'll keep my mouth shut. But it's Sunday so I'm innocent ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default

    thank you all
    all i needed to do is put the printstream outside the while loop

Similar Threads

  1. question. thanks in advance for your help
    By farahm in forum New To Java
    Replies: 4
    Last Post: 11-22-2010, 11:25 AM
  2. simple question. thanks in advance fro your help
    By farahm in forum Advanced Java
    Replies: 3
    Last Post: 11-22-2010, 10:58 AM
  3. Simple question Hopefully, Scanner/file
    By drucey in forum New To Java
    Replies: 23
    Last Post: 10-25-2010, 01:55 PM
  4. java question output file
    By dazednconfused in forum New To Java
    Replies: 3
    Last Post: 09-17-2009, 03:07 PM
  5. Question about File renameTo() method : Abnormal output
    By suvojit168 in forum New To Java
    Replies: 1
    Last Post: 07-25-2009, 08:22 PM

Posting Permissions

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