Pass a String matrix to Int and add its own values row by row

0

I'm new to Java. I have a matrix of type String of the following format:

Mara,10,10,10,10,10,10,10  
Mare,10,10,10,10,10,10,10  
Mari,10,10,10,10,10,10,10  
Maro,10,10,10,10,10,10,10  
Maru,10,10,10,10,10,10,10  

The code I have to go through is the following:

for (int x=0; x < info.length; x++){
    System.out.print("Registro número " + (x+1) + " "+ info[x][0] +":  ");
    for (int y=1; y < info[x].length; y++){
        System.out.print(info[x][y] +"\t");
    }
    System.out.print("\n");  
}

What gives me the following result:

Registro número 1 Mara:  10 10  10  10  10  10  10  
Registro número 2 Mare:  10 10  10  10  10  10  10  
Registro número 3 Mari:  8  10  10  10  10  10  10  
Registro número 4 Maro:  10 10  10  10  10  10  10  
Registro número 5 Maru:  10 10  10  10  4   10  10  
Registro número 6 alberto:  10  10  10  1   10  10  10  

Now, I change the matrix from String to Integer with the following code (I omit the column "zero" which is where the names are written, initializing "y" in 1)

for (int x=0; x < info.length; x++){  
System.out.print("Registro número " + (x+1) + " "+ info[x][0] +":  ");  
for (int y=1; y < info[x].length; y++){  
int numEntero = Integer.parseInt(info[x][y]);  
System.out.print(numEntero +"\t");  
System.out.print("\n");
}

Throwing the following result:

Registro número 1 Mara:  10 10  10  10  10  10  10  
Registro número 2 Mare:  10 10  10  10  10  10  10  
Registro número 3 Mari:  8  10  10  10  10  10  10  
Registro número 4 Maro:  10 10  10  10  10  10  10  
Registro número 5 Maru:  10 10  10  10  4   10  10  
Registro número 6 alberto:  10  10  10  1   10  10  10  

Now, how do I add the rows so that I know what the final qualification of Mara is for example?

That is to say I must add info[0][1] + info[0][2].... + info[0][7]
In other words, I must add info[1][1] + info[1][2].... + info[1][7]
etc.

And throw me something like this:

Registro número 1 Mara:  70     
Registro número 2 Mare:  70  
Registro número 3 Mari:  68  
(así hasta cubrir el total de filas de la matriz)

Someone could help me please.

    
asked by Alberto 22.11.2018 в 22:32
source

1 answer

1

You take care that a variable is adding up everything by row, try with the following modification:

for (int x=0; x < info.length; x++){  
   System.out.print("Registro número " + (x+1) + " "+ info[x][0] +":  "); 
   int t=0; 
   for (int y=1; y < info[x].length; y++)
   {  
      int numEntero = Integer.parseInt(info[x][y]);  
      System.out.print(numEntero +"\t");
      t=t+numEntero;  
   }
   System.out.print(t+"\t");
   System.out.print("\n");   
}
    
answered by 22.11.2018 / 22:51
source