HELP: What's wrong with my code - Java
Hi guys, I would really appreciate as to why my code isn't working. So, I basically want this code to randomly generate a list of numbers and then write these numbers to a file. The code to "randomly generating numbers" works perfectly fine, however, the code for "writing to a text file" doesn't write a list of numbers to a text file, but simply only one number appears and is written. Below is my code: ----------------------------------------------------------------------------------------------------------------------------------------- import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; public class ThreadB { public static void main(String[] args) throws IOException{ Random numGenerator = new Random() ; for(int counter=1; counter<=50; counter++) { int number = 1 + numGenerator.nextInt(400); System.out.println(number); PrintWriter out = new PrintWriter(new FileWriter ("random.txt")); out.println(number++); out.close(); } } } ----------------------------------------------------------------------------------------------------------------------------------------- However this persons code works, but not mine. ----------------------------------------------------------------------------------------------------------------------------------------- import java.io.FileNotFoundException; import java.io.PrintWriter; public class WriteFile { public static void main(String[] args) throws FileNotFoundException { PrintWriter out = new PrintWriter("random.txt"); int line = 1; while (line <= 100) out.println((line++) + ": " + Math.random()); out.close(); } } -----------------------------------------------------------------------------------------------------------------------------------------