0

I'm trying to create a program as described in the image linked below. I'm finding trouble with how to print the characters on the same line so that I can get 20 across and how to go to a new line after the first 20 characters have printed. Can you help me figure out a way to print the randomly selected characters into a 20 by 7 grid? Thank you for the help! Below is what I have so far but it's printing every new character on its own line and for gridArray[3] the forward-slash has to have two quotes otherwise it says that it's not a valid string. Does anyone know how I could solve these problems?

Link to Problem Directions

package edu.skidmore.cs106.lab09.problem14;
import java.util.Random;
public class GridGenorator {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] gridArray = new String[6];
        gridArray[0] = "+";
        gridArray[1] = "-";
        gridArray[2] = "/";
        gridArray[3] = "\"";
        gridArray[4] = "|";
        gridArray[5] = "_";
        for (int elementx = 0; elementx < 7; elementx++) {
            for(int elementy = 0; elementy<20; elementy++) {
                Random rand = new Random();
                int randomNum = rand.nextInt(6);
                System.out.println(gridArray[randomNum]);
            }
        }
    }

}
  • 1
    What have you done so far and what problems are you facing – Scary Wombat Oct 27 '20 at 01:57
  • [How do I generate random integers within a specific range in Java?](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) – Abra Oct 27 '20 at 02:00

1 Answers1

0

System.out.println prints a new line which you do not want to do every time, only after a group of 20, so try

    for (int elementx = 0; elementx < 7; elementx++) {
        for(int elementy = 0; elementy<20; elementy++) {
            Random rand = new Random();
            int randomNum = rand.nextInt(6);
            System.out.print(gridArray[randomNum]);
        }
        System.out.println();  // now you want to print a newline
    }
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64