need assistance with writing random array values to a file in Java

55 Views Asked by At

I am struggling with a beginning Java class. I have to modify a program to replace a user-generated integer array with a Random() number generated double precision floating point array.

This is what I have so far. I think it is generating the correct dataset, but I can't get the PrintWriter section configured correctly.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assign6array;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Random;
/**
 *
 * @author matthew.neesley
 */
public class Assign6Array {
    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {
        // TODO code application logic here
        int[] array = new int[10];
        int count = 0;
        int numbers = 0;
        Random rd = new Random(); // creating Random object
        double[] arr = new double[10];
        for (int i = 0; i < array.length; i++) {
            arr[i] = rd.nextInt(); // storing random integers in an array
            while (numbers!= -1 && count <= 9)
            {
                array[count] = numbers;
                count++;
                System.out.println(arr[i]); // printing each array element
                PrintWriter writer  = new PrintWriter(System.out);
                printr.print(arr[i]);
            }
        }
    }
}
1

There are 1 best solutions below

0
Charchit Kapoor On

You are creating PrintWriter like this

PrintWriter writer  = new PrintWriter(System.out);

The variable is named writer, but you use it like this:

printr.print(arr[i]);

Using printr variable which doesn't exist. Do simply:

writer.print(arr[i]);

Also, you need PrintStream acts as a buffer, so you will have to flush it's contents so that they are written to the output stream.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Random;

/**
 * @author matthew.neesley
 */
public class Assign6Array {

  /**
   * @param args the command line arguments
   * @throws java.io.FileNotFoundException
   */
  public static void main(String[] args) throws FileNotFoundException {
    // TODO code application logic here
    int[] array = new int[10];
    int count = 0;
    int numbers = 0;

    Random rd = new Random(); // creating Random object
    double[] arr = new double[10];
    for (int i = 0; i < array.length; i++) {
      arr[i] = rd.nextInt(); // storing random integers in an array

      PrintWriter writer = new PrintWriter(System.out);
      while (numbers != -1 && count <= 9) {
        array[count] = numbers;
        count++;
        System.out.println("abc " + arr[i]); // printing each array element
        writer.println(arr[i]);
        writer.flush();
      }
      writer.close();
    }
  }
}