PDA

View Full Version : JAVA PrintWriter question



Beaulne
February 7th, 2007, 02:36 PM
The question I have about PrintWriter is how do I use it in a loop.

What I would like to do is write to a text file 100 random ints but every time I put the output in a loop it writes nothing to to the file...

Here is my code with out the loop


import java.io.*;
import java.util.*;

public class TextIntWrite {
public static void main(String[] args) throws Exception {

// Creates the file and the PrintWriter
File file = new File(args[0]);
PrintWriter output = new PrintWriter(file);
// The Random Int Generator
Random generator = new Random();
int randomInt = generator.nextInt();


output.print(randomInt);
// Close the file
output.close();

}
}

here is the code I would think of using with a loop
import java.io.*;
import java.util.*;

public class TextIntWrite {
public static void main(String[] args) throws Exception {

// Creates the file and the PrintWriter
File file = new File(args[0]);
PrintWriter output = new PrintWriter(file);
// The Random Int Generator
Random generator = new Random();
int randomInt = generator.nextInt();

int count =0;

while (count ==100){
output.print(randomInt);
count++;
}
// Close the file
output.close();

}
}


This prints the file but with nohting in it.


Thanks

Krilnon
February 9th, 2007, 05:32 PM
int count =0;

while (count ==100){
output.print(randomInt);
count++;
}

That should probably be <= or !=. At the moment, count will always be 0.