javacharacter-arrays

Create a random coding array using the alphabet and numbers from ASCII with a specified frame and no repeats in Java


I am coding in java for four months now - still a newbie! I want to create a randomised no-repeating array in Java with a frame to find coordinates. I have initialized the chars that I would like in the array however I persistently get stops, dashes and brackets etc in the print out. What am I missing? I do not want to go down the employing of libraries route.

final char[] randomSquare = {'C', 'O', 'D', 'I', 'N', 'G'};
// the Grid used that will be randomly filled
char[][] grid;

int A = 65;
int Z = 90;
int num0 = 48;
int num9 = 57;

// create a Random numbers generator
Random ran = new Random();

grid = new char[randomSquare.length][randomSquare.length];

// loop to print randomSquare header for grid array
for (int j = 0; j < 1; j++) {
    System.out.print("   ");
    System.out.print(randomSquare);
}   
System.out.println();

// print randomSquare at position 0 all the way down
for(int i = 0; i < randomSquare.length; i++) {
    // the letter at the beginning of the row
    System.out.print(randomSquare[i] + "  ");

    // the Grid contents for that line
    int index = 0;
    for(int j = 0; j < randomSquare.length; j++) {

        if (A >= 65 && A <= 90){

            index = ran.nextInt(A + Z);
        }

        if (num9 <= 57 && num9 >= 48) {

            index = ran.nextInt(num0 + num9);
        }
        System.out.print("" + (char)index);
    }
    System.out.println();
    index++;
}
return grid;

Solution

  • below fix your problem with chars outside A-Z and 0-9

    for(int i = 0; i < randomSquare.length; i++) {
            // the letter at the beginning of the row
            System.out.print(randomSquare[i] + "  ");
    
            // the Grid contents for that line
            int index = 0;
            for(int j = 0; j < randomSquare.length; j++) {
    
                if(ran.nextBoolean()){
                if (A >= 65 && A <= 90){
    
                    index = A + ran.nextInt(Z - A);
                }
                }else
                if (num9 <= 57 && num9 >= 48) {
    
                    index = num0 + ran.nextInt(num9 - num0);
                }
                System.out.print("" + (char)index);
            }
            System.out.println();
            index++;    }
    
        for(int i = 0; i < randomSquare.length; i++) {
            // the letter at the beginning of the row
            System.out.print(randomSquare[i] + "  ");
    
            // the Grid contents for that line
            int index = 0;
            for(int j = 0; j < randomSquare.length; j++) {
    
                if(ran.nextBoolean()){
                if (A >= 65 && A <= 90){
    
                    index = A + ran.nextInt(Z - A);
                }
                }else
                if (num9 <= 57 && num9 >= 48) {
    
                    index = num0 + ran.nextInt(num9 - num0);
                }
                System.out.print("" + (char)index);
            }
            System.out.println()
    
    ;
            index++;    }