0

I'm trying to get the console to output 100 random numbers between 0 and 50, all on the same line with a space between each. I have everything but the formatting for the space. I know I need to use the printf function, but am completely lost on how to properly impliment it. This is what I have so far, but the output formatting is incorrect.

public static void main(String[] args) {
  Random rand = new Random();
  for (int count = 0; count <=100; count++)
  {
    int randomNum = rand.nextInt(51);
    System.out.printf("%1d %1d", randomNum, randomNum);
  }
}
pjs
  • 18,696
  • 4
  • 27
  • 56
Aaron Hall
  • 29
  • 4
  • 2
    By not printing while you're still collecting numbers. Generate your numbers, put them in an `int[100]` and then turn the array into a string using `Arrays.toString` and replacing the commas with spaces. Or, alternatively, print your number followed by a space, because that last space added to the last number won't matter. – Mike 'Pomax' Kamermans Sep 03 '22 at 21:43
  • What is your output supposed to look like? For example, would you want something similar to `10 21 10 5 46 43 27 23 30 32 50 40 35 45 30 47 7 30 9 15 17 25 41 31 37 7 43 49 41 7 3 40 46 37 17 16 20 21 25 26 27 30 11 0 34 40 17 23 35 50 49 5 31 42 45 31 30 49 30 18 44 0 24 30 8 7 43 39 26 39 37 29 5 47 18 41 1 17 2 46 48 33 45 8 23 49 27 39 28 14 19 36 40 5 10 35 46 26 29 0 39`? – Water Sep 03 '22 at 21:45
  • @Mike'Pomax'Kamermans I understand that an array would work, but why doesn't OP's method work? When I tried it I got 10 34 45 1345 44. Why does the space get added sometimes, but other times (like with 1345) it's not there? – Darkshadowtrail Sep 03 '22 at 21:56
  • 1
    @Darkshadowtrail I Don't know what you tried, but what you say is not possibly what this code can produce. Assuming the randomly generated numbers so happen to be 0, 1, 2, etc, you would get `0 01 12 23 34 45 56 67 78 89 910 1011 11...` and so on (it prints the generated number, a space, the exact same number, and then the next loop will immediately follow on, no space, with the next, in the same way). – rzwitserloot Sep 03 '22 at 22:01
  • Removed the `random` tag, the question remains unchanged whether the elements are random or not. It’s about formatting, not generating randomness. – pjs Sep 04 '22 at 16:05

2 Answers2

1

Here's a version neither using a condition or a separate first print but avoiding any leading or trailing space.

public static void main(String[] args) {
    Random rand = new Random();
    String delim="";
    for (int count = 0; count <100; count++)//fixed as per comments elsewhere.
    {
        int randomNum = rand.nextInt(51);
        System.out.printf("%s%1d", delim,randomNum);
        delim=" ";// Change this to delim="," to see the action!
    }
}

It's a classic faff to print out n items with n-1 internal separators.

PS: printf feels like overkill on this. System.out.print(delim+randomNum); works just fine.

Persixty
  • 8,165
  • 2
  • 13
  • 35
-1

[1] Your code actually prints 101 numbers. Embrace the logic computers (and java) applies to loops and 'the fences' (the start and end): The first number is inclusive, the second is exclusive. By doing it that way, you just subtract the two to know how many items there are. so, for (int count = 0; count < 100; count++) - that loops 100 times. Using <= would loop 101 times.

[2] You're making this way too complicated by focusing on the notion of 'there must be a space in between 2', as if the 2 is important. What you really want is just 'after every random number, print a space'. The only downside is that this prints an extra space at the end, which probably doesn't matter:

for (int count = 0; i < 100; count++) {
  System.out.print(randomNum + " ");
}

is all you actually needed. No need to involve printf:

I know I need to use the printf function

No, you don't. No idea why you concluded this. It's overkill here.

If you don't want the extra space.. simply don't print it for the last number:

for (int count = 0; i < 100; count++) {
  System.out.print(randomNum);
  if (count < 99) System.out.print(" ");
}

[3] You mention that the code shuold print it all 'on one line', which perhaps suggests the line also needs to actually be a line. Add, at the very end, after the loop, System.out.println() to also go to a newline before you end.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • you definitely want to fix those code blocks a little, because you've mixed `count` and `i`, leading to either an infinite loop, or just a compile error because `i` is undefined. And it's printing the same `randomNum` every iteration, rather than including the reassignment. – Mike 'Pomax' Kamermans Sep 04 '22 at 15:34
  • The snippets hint at _how_ to do it. They aren't meant to just 'copy/paste this, then hand in your homework' (I'm just guessing this is homework, but it certainly looks like this). I'm pretty sure that serving up homework on a silver platter is a lose/lose situation (student doesn't learn, SO gets inundated with low-quality questions, teachers can't properly gauge what to teach and who to focus on, SO gets inundated even more, given that the askers are trained to just expect nothing less than an answer that is copy/paste ready with no modifications). – rzwitserloot Sep 04 '22 at 18:13
  • In other words, no, I definitely don't. Perhaps my judgement on what leads to win/win situations is off, in which we case we should discuss (in chat, presumably, not as comments on this question). But assuming you agree with em, then the answer as it stands now is in fact _better_ than including all the things you mentioned. – rzwitserloot Sep 04 '22 at 18:14
  • In that case it might be time to reread [how to answer questions](/help/how-to-answer) because that's simply not in line with what a good answer looks like as per our guidelines. – Mike 'Pomax' Kamermans Sep 04 '22 at 18:58