I have created a loop below that will display around 50 numbers 'at random' between 1 & 999. The problem I have is that I need to print out the entire array outside the loop (as attempted) but I need it in the same format I have printed it within the loop.
I have tried a few ways of doing it, but it keeps throwing errors, mainly to do with 'illegal conversions'.
// Imports
import java.util.Arrays;
import java.text.DecimalFormat;
// Class
public class Random50
{
public static void main(String[] args)
{
// Declaration
double[] Random50Array = new double[51];
DecimalFormat df = new DecimalFormat("000");
int i;
// Loops
for(i = 0; i < Random50Array.length; i++)
{
Random50Array[i] = (int)(Math.random() * 999);
System.out.print(df.format(Random50Array[i]) + ", ");
}
System.out.println("");
System.out.println("");
String RandomArray = (Arrays.toString(Random50Array));
System.out.printf("%03d", RandomArray);
}
}
I appreciate any future guidance given. :)
RandomArrayis a String object, while %d signifies a decimal integer. That's why you are getting Illegal format exception.