1

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. :)

3
  • 1
    you've managed to violate almost all of the Java conventions for code formatting, and your Java looks like C! -- no but in seriousness, which lines are throwing the exception, and you should post the stack trace as well. Commented Nov 30, 2014 at 5:35
  • RandomArray is a String object, while %d signifies a decimal integer. That's why you are getting Illegal format exception. Commented Nov 30, 2014 at 5:35
  • @AliAmiri: clearly, OP wants to take up three digits for all numbers (e.g. 24 --> "024"), which can't be done with System.out.print. Commented Nov 30, 2014 at 5:38

2 Answers 2

2

You could append the formatted strings within the loop together, and print them out all at once at the end.

// ...
StringBuilder builder = new StringBuilder();
for(i = 0; i < Random50Array.length; i++)
{
  Random50Array[i] = (int)(Math.random()*999);
  String output = df.format(Random50Array[i])+ ", ";
  System.out.print(output);
  builder.append(output);
}

System.out.println("");
System.out.println("");
System.out.print(builder.toString());

Note that you shouldn't use System.out.printf("%03d", "..."); to print strings, since the "%03d" means that the argument you are passing is a number. This is the cause of the errors you are experiencing.

Sign up to request clarification or add additional context in comments.

Comments

0

Optimized Code: You do not need double array, don't you

 StringBuilder builder = new StringBuilder();
for(i = 0; i < Random50Array.length; i++)
{
  String output = df.format((int)(Math.random()*999)+ ", ";
  builder.append(output);
}

System.out.println("");
System.out.println("");
System.out.print(builder.toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.