Input / Output Java -
my program supposed write 100 random integers on text file , read them. problem i'm printing 1 integer. know i'm close. doing wrong?
import java.util.random;  public class writedata {         public static void main(string[] args) throws exception {         //create file instance         java.io.file file = new java.io.file("random100.txt");         if (file.exists()) {             system.out.println("file exists");             system.exit(0);         }         //create file         java.io.printwriter output = new java.io.printwriter(file);         //write formatted output file         random randomgenerator = new random();         (int idx = 1; idx <= 100; ++idx) {             int randomint = randomgenerator.nextint(100);             output.print(randomgenerator);             //log("generated : " + randomint);             //close file              output.close();         }     } } import java.util.scanner;  public class readdata {      public static void main(string[] args) throws exception {         //create file instance         java.io.file file = new java.io.file("random100.txt");         //create scanner file         scanner input = new scanner(file);         //read data file          while (input.hasnext()) {              int number = input.nextint();             system.out.println(number + " ");         }         //close file         input.close();     } } 
output.close(); should outside loop. right now, loop executes once , outputstream gets closed. hence, 1 number.
Comments
Post a Comment