Matrix containing numbers and names

0

I am just learning to program in java, in blueJ, I need to create a code to calculate the salary of workers and in the end print the data of each worker (name, rut, contract, etc) in a matrix, I have been trying to do the matrix to print 4 data, but the blueJ throw me just nullnullnullnull, I do not know what to do to make it work or how I have to do so to print a matrix that has numbers and characters, I leave my code

public class Trabajador

{

public static void getDatos (){
    String rut;
    String nombre ;
    String contrato;
    Scanner leer = new Scanner(System.in);
    System.out.println("Ingrese numeros de trabajadores");
    int j = leer.nextInt();
    String matriz [][] = new String [j][4];
    int y=0;

for(int i=0; i < j; i++){
     System.out.println ("Ingrese nombre del trabajador");
     nombre = leer.next();
     nombre = matriz [i][0];
     System.out.println("Ingrese el rut sin guión");
     rut = leer.next();
     rut = matriz [i][1];
     System.out.println("¿Que tipo de contrato tiene? a.partTime b.Contrato c.Planta");
     contrato = leer.next();
     contrato = matriz [i][2];
    }
for (int x=0; x < matriz.length; x++){
  for(y=0; y < matriz[x].length; y++){
    System.out.print(matriz[x][y]);


  }
     }

} }

    
asked by CHRISTIAN MARCELO HERRERA ROJA 04.11.2017 в 16:03
source

1 answer

0

Christian, I tried the code in netbeans, another IDE, the changes to make are:

1.- The length of the matrix must be [j] [3], since there are 3 data that you ask for (name, rut and type of contract)

2.- The program throws null because you are assigning the variables name, rut and contract, the matrix, and it is the other way around. It should look like this when it's fixed:

         matriz [i][0] = nombre;
         matriz [i][1] = rut;
         matriz [i][2] = contrato;

Greetings and luck:)

    
answered by 05.11.2017 / 00:24
source