I would like to know why this code:
import java.io.*;
import java.util.Random;
public class RandomNumbers {
public static void main(String[] args) throws IOException {
PrintWriter output = new PrintWriter("randomNumbers.txt");
final int randomNumberCount = 100;
Random randomGenerator = new Random();
for (int i = randomNumberCount; i >= 0; i--)
{
output.print(randomGenerator.nextInt(10) + " ");
}
output.close();
}
}
Is giving me a text file with this:
‰‹‸‸‹‵‹‴‶′′‴‷″‵‷‴′‶‷‵‹‷′‱″″‸‰‷‸′″‵‹″′‶‶‴‶‴‸′″‹‶‱‱‰‸‸‱‷‶‹‶‶‵‰‹‰‰‹‱‸‷‱‵‶‵‷′‱‵‵‸‸‵‵‱‸‷‸‸‱‸‱‶‱‸″‸′‶″‸‸‷‶′
When I change it to pick random numbers of 11 or above, it works.
If I keep it at 10 and make it pick 48 random numbers or lower it works.
What am I missing?
EDIT
It will show up correct in Notepad++ but not in Notepad.
In the future, trying A: Specifying the charset to use (I’m guessing notepad doesn’t support the default PrintWriter one, only UTF-8). or B: Using different types of OutputStreams, as it appears PrintWriter is using an odd charset, which Notepad doesn’t recognize, hence the random funky characters.