Results 1 to 3 of 3
  1. #1
    marylanddem is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default Program compiles but wont run to text file...

    I'm trying to create a java program that writes 10 random integers (negative and positive) on a document called numbers.txt. The program compiles, but its not writing to the file. Any insight as to why not?

    Java Code:
    import java.util.*;
    import java.util.Random;
    import java.io.*;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class sort5
    {
    public static void main(String[] args) throws java.io.IOException
    {
    Random generator = new Random();
    Random generator1 = new Random();
    int[] intarr = new int[10];
    int toggle;
    File outFile;
    PrintWriter pw;
    String filename = "numbers.txt";
    
    
    for (int i=0; i < intarr.length; i++)
    {
    toggle = 1 + generator.nextInt(2);
    
    if (toggle == 1)
    intarr[i] = -1 * generator.nextInt(32767);
    else
    intarr[i] = generator.nextInt(32767);
    }
    
    outFile = new File(filename);
    
    if (! outFile.exists()) {
    
    pw = new PrintWriter(new BufferedWriter(new FileWriter(outFile)));
    for (int k=0; k<=intarr.length; k++){
    pw.println(k);
    pw.flush();
    pw.close();
    }
    }
    Last edited by Fubarable; 12-05-2010 at 04:04 PM. Reason: Moderator Edit: Code tags added

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

    Default

    Quote Originally Posted by marylanddem View Post
    for (int k=0; k<=intarr.length; k++){
    pw.println(k);
    pw.flush();
    pw.close();
    }
    Have a close look at this part of your code: it writes 0 the first time in the loop body but in that same pass you flush and close the stream. I'm sure your code throws an exception when it enters the loop body for a second time.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    I added code tags, but your code is not well formatted to begin with. I suggest you edit the original post and post well-formatted code inside of the tags.

    Also, you will want to get rid of this:
    Java Code:
    if (!outFile.exists()) {
    since that will prevent you from writing to the file again. You will need to search the user dir for the file, because it likely does exist.

Similar Threads

  1. This program compiles but doesnt run properly!
    By ErikD99 in forum New To Java
    Replies: 5
    Last Post: 12-03-2010, 08:44 PM
  2. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  3. Neec a help in this program (printing RGB into text file)
    By Hafsa Hosani in forum New To Java
    Replies: 2
    Last Post: 03-24-2009, 12:35 PM
  4. Replies: 4
    Last Post: 01-01-2009, 03:58 AM
  5. Program Compiles but Buttons do not display
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-11-2008, 03:41 AM

Posting Permissions

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