Jump to content

Java Programming Help Needed (IO, Random)

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Jason Marquez

Jason Marquez

    Newbie

  • Members
  • PipPip
  • 28 posts
I need help programming a java program which creates 100 random numbers and writes it to a text file named "Exercise9_19.txt". If the file exists then it justs adds onto it, if the file does not exist, it will create the text file and begin placing the random numbers.

This is what I've done so far... I'm having trouble writing it to the file.



import java.io.*;

import java.util.Random;


public class Question3

{

    public static void main(String[] args) throws IOException

    {

        Random rand = new Random();


        FileWriter outFile = new FileWriter(args[0]);

        PrintWriter out = new PrintWriter(outFile);


        File file = new File("Exercise9_19.txt");


        for( int counter = 0; counter < 100;  counter++ )

        {

            int a = rand.nextInt(); //Makes random numbers fine

            out.println(a); //Need to fix how to take "a" and place it onto the text file

        }


        out.close();

    }

}



#2
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
You thread will receive enough attention without double posting.
You may want to look at the constructors in this class for starters. FileWriter (Java 2 Platform SE v1.4.2). As is you cannot append.

---------- Post added at 10:47 PM ---------- Previous post was at 10:25 PM ----------

Edit: This may be helpful on more than one occasion.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users