Error with data types in Java when compiling

1

I have a problem trying to compile this exercise in Java, I use the console to compile and the error it throws at me is something like this: Can not convert the data type String to String [] [] How should I Then make the statements?

/*
    Crea un programa que cree un array de 5x5 caracteres, lo rellene con puntos, 
    excepto la segunda fila, que estará rellena de letras "A" y luego muestra el 
    contenido del array en pantalla. Deberá aparecer algo como: 
    ..... 
    AAAAA 
    ..... 
    ..... 
    .....
*/
    class ejercicio58 {
        public static void main(String[] args) {
            String[][] m = new String[5][5];
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    if(i==1) 
                        m[j] = "A";
                    else 
                        m[j] = ".";
                }
                if(i==1) 
                    m[i] = "A";
                else
                    m[i] = ".";
            }
            for (int i = 0; i<5; i++) {
                System.out.print(m[i]);
                for (int j = 0; j<5; j++) {
                    System.out.print(m[j]);
                }
                System.out.println();
            }       
        }
    }
    
asked by Pedro Fumero 12.03.2017 в 18:13
source

2 answers

3

The problem is that the string array you have created is 2-dimensional (5x5) and in the assignments you take it as if it were one. For example, in the first assignment you make, within the for:

if(i==1) 
    m[j] = "A";
else 
    m[j] = ".";

you would miss the i:

if(i==1) 
    m[i][j] = "A";
else 
    m[i][j] = ".";

The same thing happens to you when you want to print it. Try it and tell me;) Greetings.

    
answered by 12.03.2017 / 19:07
source
3

To assign a value to a two-dimensional array, you have to indicate in which row and column the value goes.

Example:

The first column of the array, which is located in [0], would be filled like this:

m[0][0] = "."; //Fila 1  columna 1
m[1][0] = "A"; //Fila 2, columna 1
m[2][0] = "."; //Fila 3, columna 1
m[3][0] = "."; //Fila 4, columna 1
m[4][0] = "."; //Fila 5, columna 1

The second column, found in [1],:

m[0][1] = "."; //Fila 1  columna 2
m[1][1] = "A"; //Fila 2, columna 2
m[2][1] = "."; //Fila 3, columna 2
m[3][1] = "."; //Fila 4, columna 2
m[4][1] = "."; //Fila 5, columna 2

etc.

Then, in a for loop, it would be:

import java.util.*;
import java.lang.*;
import java.io.*;

class ArregloCinco 
{
  public static void main(String args[]) 
  {
   // Crear array 5 x 5
    String[][] m = new String[5][5];

    for (int i = 0; i < m.length; i++) {
        for (int j = 0; j < m[0].length; j++) {
            //Fila 2
            if (i==1) {
                m[i][j] = "A";
            }else{
                m[i][j] = ".";

            }
        }
    }

    //Imprimir
    for (String[] array : m) 
    {
        for (String element : array)
        {
                System.out.print(element);
                System.out.print(" ");
        }
        System.out.println();
    }
  }
}

Result

. . . . . 
A A A A A 
. . . . . 
. . . . . 
. . . . .
    
answered by 12.03.2017 в 19:39