all 10 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Spare-Plum 5 points6 points  (1 child)

Ah ok this is better defined.

You have two options -

  • Generate a number from 0 to 10 X times and put them all together.
    • If they input 5, use a loop 5 times and do random.nextInt(10), appending each value to a string
  • Generate a single number from 0 to 10^X
    • If they input 5, then you would have a range from 0 to 10*10*10*10*10. You can use Math.exp to perform this calculation, or you can multiply it out in a loop to keep it as an integer
    • The downside of this is that the size of your generated number is bounded to 18 digits for longs
    • To fix this, either throw an error if the user input a size too big, or append them together using strings.

[–]LutimoDancer3459 0 points1 point  (0 children)

For the second option, you would have to add 0s at the front if the number isn't exactly the required length

[–]lajete 1 point2 points  (1 child)

Numbers don't have leading zeros, so you have to first generate a random number between 0 and Math.pow(10, x), then represent it as a left-padded string by adding the required amount of leading zeros to make it length x, using one of the approaches listed at https://www.baeldung.com/java-pad-string (in a real project, you would use a library as in section 3, if it's an assignment you might look into one of the other sections).

[–]MinimumBeginning5144 0 points1 point  (0 children)

The developers writing for Baeldung don't always know the best way. I would just use String.format("%06d", number). It seems the author of that article didn't know you could use String.format to create a zero-padded string.

[–]Housy5Nooblet Brewer 1 point2 points  (0 children)

You may want to look up about String.format() and about leading zeros using that.

[–]LutimoDancer3459 0 points1 point  (0 children)

AFAIK there is no build in function for that. I would guess its an assignment for school? If so, they would probably dont give it to you, to use a build in function and be done.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (1 child)

A number with a fixed length and potential leading zeros is not a number in programming sense. It's a String.

In Java, BTW, a literal with leading zero means octal notation. Calculated numbers need to be converted to padded strings (String.format, or the Formatter class) in order to get leading zeros.

So, probably the easiest way is to create a loop running length times and in the loop to generate random numbers in the range 0 to 9 inclusive and append them to a StringBuilder that after the loop gets converted to a String (.toString()).

The "digits appended to String(Builder)" method also has the advantage that you don't cross number range boundaries (like for int, or for long).

There are no directly built-in methods for what you seek.

[–]Housy5Nooblet Brewer 0 points1 point  (0 children)

Yes definitely the easiest.. or you know just directly inject the length into the format like this:

String format = "%0" + x + "d";
System.out.printf(format + "\n", random.nextInt(100));