-2

Hello and thank you for reading this. I have written code that fills a 2d triangular array, but it only fills with lower case letters. How do I get it to also fill with uppercase letters?

Here is a small part of the code:

public static void fillSpecialMatrice(char[][] matrice) {
   Random random = new Random();
   for(int i = 0; i < matrice.length; i++) {
      for(int j = 0; j < matrice.length-i; j++) {
         matrice[i][j] = (char) (random.nextInt(26) + 'a');
      }
   }
}

How the triangular matrix looks when printed:

enter image description here

Idle_Mind
  • 35,854
  • 3
  • 28
  • 38
Leksus
  • 1

2 Answers2

1

You can randomize upper and lower-cases as well. Something like this:

public static void fillSpecialMatrice(char[][] matrice) {
    Random random = new Random();
        for(int i = 0; i < matrice.length; i++) {
            for(int j = 0; j < matrice.length-i; j++) {
                   case_int = random.nextInt(2);
                   case_char = (case_int == 0 ) ? 'a' : 'A';
                   matrice[i][j] = (char) (random.nextInt(26) + case_char);
     }
    }
  }
vish4071
  • 4,935
  • 2
  • 31
  • 61
-1

ThreadLocalRandom offers methods making it more convenient than Random.

The char type has been legacy since Java 5, essentially broken since Java 2. As a 16-bit value, char is physically incapable of representing most characters. Instead, use code point integer numbers to represent individual characters.

The Character class offers static methods toUpperCase & toLowerCase. By the way, the class also offers toTitleCase; see Difference between uppercase and titlecase.

The word “matrix” is the singular of “matrices”, in English.

Putting that all together, we get code that looks like the following.

final int origin = Character.codePointAt( "a" , 0 );
final int bound = origin + 26;  // Twenty-six characters in US English alphabet, in US-ASCII subset of Unicode.
final int rows = 7, columns = 7;
final String[][] matrix = new String[ rows ][ columns ];

for ( int i = 0 ; i < matrix.length ; i++ )
{
    for ( int j = 0 ; j < matrix.length - i ; j++ )
    {
        int codePoint = ThreadLocalRandom.current().nextInt( origin , bound );
        // Randomly use uppercase rather than default lowercase.
        if ( ThreadLocalRandom.current().nextBoolean() )
        {
            codePoint = Character.toUpperCase( codePoint );
        }
        matrix[ i ][ j ] = Character.toString( codePoint );
    }
}

Dump to console.

System.out.println( Arrays.deepToString( matrix ) );

for ( int rowIndex = 0 ; rowIndex < matrix.length ; rowIndex++ )                            // Rows.
{
    for ( int columnIndex = 0 ; columnIndex < matrix[ rowIndex ].length ; columnIndex++ )   // Columns
    {
        String cell = matrix[ rowIndex ][ columnIndex ];
        String output = Objects.isNull( cell ) ? " " : cell;  // Ternary operator. Effectively a compact if-else statement. 
        System.out.print( output + " " );
    }
    System.out.println();                                 // Next line.
}

When run.

[[a, P, F, B, u, R, j], [G, o, F, H, z, L, null], [N, R, f, y, m, null, null], [U, f, F, j, null, null, null], [R, G, i, null, null, null, null], [G, b, null, null, null, null, null], [g, null, null, null, null, null, null]]
a P F B u R j 
G o F H z L   
N R f y m     
U f F j       
R G i         
G b           
g             
Basil Bourque
  • 262,936
  • 84
  • 758
  • 1,028
  • What is the point of using codepoints instead of `char` values, when the only values required are a-z and A-Z? When you say `char` can't represent most characters, that's a bit misleading. It can represent every character that's currently on this page, and many, many more besides. It's only in very specialised domains that it's insufficient. The reality is, that out in the real world, almost everyone uses `char` to store characters, so Java programmers need to learn how to use `char`. So the codepoint business is a complete red herring. The `ThreadLocalRandom` is also a red herring - – Dawood ibn Kareem Mar 03 '22 at 23:40
  • it's clearly not required for this exercise. And you haven't done anything with it that `Random` can't do. As for the spelling of `matrix`, who cares? You've deliberately answered a comparatively simple question in the most complex and irrelevant way you possibly could have; and have thus earned my downvote. – Dawood ibn Kareem Mar 03 '22 at 23:41