0

I'm trying to make a 3-letter name generator for Minecraft, and I was planning on sharing this with my non-existent friends.

package bored;
import java.util.Random;
public class Yay {

    public static void main(String[] args) {
        String[] arr={"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_"};
        int i;
        int f;
        for (f=0; f<10; f++) {
            for (i=0; i<3; i++) {
                Random r=new Random();
                int randomNumber=r.nextInt(arr.length);
                System.out.print(arr[randomNumber]);
            System.out.println("");
            }
        }
    }
}

It should print something like "3ab" "9dl" but instead it prints

3

a

b

9

d

l

3
  • 2
    you want to move the call to System.out.println(""); outside the inner for-loop. Sep 10, 2019 at 22:33
  • unfortunately i already tried that and it didn't work Sep 10, 2019 at 22:35
  • I posted an answer which seems to work when I run it. Sep 10, 2019 at 22:36

4 Answers 4

1

You're currently calling the newline print statement inside the inner for loop. You can change the code from this

for (f=0; f<10; f++) {
    for (i=0; i<3; i++) {
        Random r=new Random();
        int randomNumber=r.nextInt(arr.length);
        System.out.print(arr[randomNumber]);
    System.out.println("");
    }
}

to this

for (f=0; f<10; f++) {
    for (i=0; i<3; i++) {
        Random r=new Random();
        int randomNumber=r.nextInt(arr.length);
        System.out.print(arr[randomNumber]);
    }
    System.out.println();
}
1

You need to move the System.out.println(""); outside of the inner for-loop.

Also I would not init the f and i variables outside your for-loop because you only need them inside:

public static void main(String[] args) {
    String[] arr={"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_"};

    for (int f=0; f<10; f++) {
        for (int i=0; i<3; i++) {
            Random r=new Random();
            int randomNumber=r.nextInt(arr.length);
            System.out.print(arr[randomNumber]);
        }
        System.out.println("");
    }
}
0

The issue is you're calling System.out.println(""), inside the innermost for loop. Based on your expected output and indentation, I suspect you meant to write:

import java.util.Random;

public class Yay {

    public static void main(String[] args) {
        String[] arr={"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_"};

        for (int f=0; f<10; f++) {
            for (int i=0; i<3; i++) {
                Random r=new Random();
                int randomNumber=r.nextInt(arr.length);
                System.out.print(arr[randomNumber]);
            }
            System.out.println(); // Outside of the for loop
        }
    }
}
0

You must move `System.out.println(""); outside the inner for loop.

import java.util.Random;
public class Main {

    public static void main(String[] args) {
        String[] arr={"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_"};
        int i;
        int f;
        for (f=0; f<10; f++) {
            for (i=0; i<3; i++) {
                Random r=new Random();
                int randomNumber=r.nextInt(arr.length);
                System.out.print(arr[randomNumber]);

            }
            System.out.println("");
        }
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.