Im new to Java and I’m having a little problem writing a series of random number to an output file. I need to used RandomAccessFile and writeDouble. Here is that pice of my code any idea why this is happening. Thanks
private static void numGenerator(int values){
Random generator = new Random();
for (int i = 0; i < values; i++) {
double number = generator.nextInt(200);
System.out.println(number);
String outFile = "output.txt";
RandomAccessFile outputStream = null;
try{
outputStream = new RandomAccessFile(outFile,"rw");
}
catch(FileNotFoundException e){
System.out.println("Error opening the file " + outFile);
System.exit(0);
}
number = outputStream.writeDouble(number); //ERROR
}
}
EDIT:
Error: Type mismatch: cannot convert from void to double
The error makes sense. You’re writing into a RAF, and per its API the writeDouble method returns void. Why are you trying to set a number equal to this? This statement makes no sense:
Instead just do:
Also, why create a new RAF with each iteration of the for loop? Don’t you instead want to create one file before the for loop and add data to it inside of the loop?
Also, why use a RAF to begin with? Why not simply use a text file?