0

I want to convert some integers into a * based on a rule. I can determine which places in the matrix it should convert, but cannot convert it. I give the program the first matrix and it should return the second matrix:

5 4 5 5 8
5 4 6 4 1
3 4 5 4 6
7 8 4 3 6

5 4 5 5 *
5 4 * 4 1
3 4 5 4 6
7 * 4 3 6

my code is this:

for(int i=1; i<r-1; i++) {
  for(int a=1; a<c-1; a++){
    if(matrix[i-1][a] < matrix[r][a] && matrix[i+1][a] < matrix[r][a] && matrix[i][a-1] < matrix[r][a] && 
       matrix[i][a+1] < matrix[r][a]) {
      matrix[r][a] = *;
    }
  }
}

Edit: The matrix is an int type. I can determine which locations in the matrix should be converted, however the convertion itself does not work.

I get this error: Error: Syntax error on token "*", invalid Expression

T.v.L.
  • 43
  • 8

4 Answers4

1

You could leave int[][] matrix, and e.g. mark a * cells with some illegal number (e.g. Integer.MAX_VALUE). And when you print or use this matrix, do like: System.out.print(matrix[i][k] != Integer.MAX_VALUE ? matrix[i][k] : '*')

public static void main(String... args) {
    int[][] matrix = new int[5][5];
    modifyMatrix(matrix);
    print(matrix);
}

public static void modifyMatrix(int[][] matrix) {
    for (int row = 0; row < matrix.length; row++)
        for (int col = 0; col < matrix[row].length; col++)
            if (isStar(row, col, matrix))
                matrix[row][col] = Integer.MAX_VALUE;
}

private static boolean isStar(int row, int col, int[][] matrix) {
    // TODO your condition to place a '*'
    return false;
}

private static void print(int[][] matrix) {
    for (int row = 0; row < matrix.length; row++) {
        for (int col = 0, max = matrix[row].length; col < msx; col++) {
            System.out.print(matrix[row][col] != Integer.MAX_VALUE ? matrix[row][col] : '*');
            System.out.println(col != max - 1 ? ' ' : '\0');
        }
        System.out.println();
    }
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
1

you can use matrix of type Integer instead of int and then mark a * cell with null

Then when printing or using matrix show null values with *

ygngy
  • 3,630
  • 2
  • 18
  • 29
  • [What could possibly go wrong?](https://384uqqh5pka2ma24ild282mv-wpengine.netdna-ssl.com/wp-content/uploads/2016/06/Top10Exceptions-1.png) – Turing85 May 03 '18 at 20:38
  • 1
    @Turing85 with a simple `if` the `null` value could be checked. the benefit of `null` value is that it is always invalid value you may need any other supposed invalid value in the future such as `Integer.MAX_VALUE` or others – ygngy May 03 '18 at 20:41
  • I prefer to use `int` instead of `Integer` where is is possible – Oleg Cherednik May 03 '18 at 20:41
  • Of course. That is true for ever `null`. Yet, the `NPE` is the most prominent exception and there are languages, like Kotlin, explicitly trying to add `null`-safety. – Turing85 May 03 '18 at 20:42
0

In an int matrix you can't put characters or symbols(like '*')

The easiest thing to do is to change the type of the matrix. You should use char instead of int.

  char[][] matrix = new char[r][c];

If you want to use an element from the matrix for equations you can use the

Character.getNumericValue(matrix[i][j]);

With this code it will return the number and if it has not numeric value it will return -1

You can check this link for the getNumericValue click here

Mike Bean
  • 3
  • 5
  • And what if we want **numbers with more than one digit**? for example `23` this is two characters or `789` is three characters so we have to use `String` instead of `char` – ygngy May 04 '18 at 10:26
  • The example that he gave us is only a digit. if he wants to use more than one characters he can use **String[][]** but he has to change a little the code. Instead of Character.getNumericValue(matrix[i][j]); he can use a for(int k =0; i – Mike Bean May 07 '18 at 11:21
0

you should create an new array of strings the size of your matrix.

then run trough your matrix and either add the number or change it into a string, like this:

public String[] convert(int[] matrix,int rule) {
    String[][] arr = new String[matrix.length][matrix[0].length];
    for(int i = 0;i<matrix.length;i++) {
        for(int j = 0; j<matrix[i].length;j++) {
            if(matrix[i][j] == rule)
                arr[i][j] = "*";
            else
               arr[i][j] = String.valueOf(matrix[i][j]);
    return arr;
javajav
  • 379
  • 3
  • 10