I want to have the following outcome:
and i was writing the following code for this purpose:
String[][] teamView = new String[24][35];
int [] numbers = new int[]{};
int k =1;
for(int i=0; i< 24; i++)
{
for(int j = 0 ; j< 35; j++)
{
if (j == 0 && i==0){teamView[i][j] = "@";}
else if(j==0){ teamView[i][j] += (char)(i + 64) ;}
else if (j==1){teamView[i][j] = " " ;}
else if (i ==0 ){
switch (j){
case 3:
teamView[i][j] = "1" ;
break;
case 6:
teamView[i][j] = "2" ;
break;
case 9:
teamView[i][j] = "3" ;
break;
case 12:
teamView[i][j] = "4" ;
break;
case 15:
teamView[i][j] = "5" ;
break;
case 18:
teamView[i][j] = "6" ;
break;
case 21:
teamView[i][j] = "7" ;
break;
case 24:
teamView[i][j] = "8" ;
break;
case 27:
teamView[i][j] = "9" ;
break;
case 30:
teamView[i][j] = "10" ;
break;
}
}else if (i ==0){teamView[i][j] = " ";}else
teamView[i][j] = "#";
System.out.print(teamView[i][j]);
}
System.out.print("\n");
}
but the problem is that now i get null in-between the numbers in the first row, also before the letters in the first column. why do i get these nulls in the print? how can i improve my loops?
it is about the battleship game in EDX(https://courses.edx.org/courses/course-v1:PurdueX+CS180.4x+1T2020a/courseware/7e1459f3e5be4579b645cf16c4196954/a030e2346c374159b8875682791e1606/3?activate_block_id=block-v1%3APurdueX%2BCS180.4x%2B1T2020a%2Btype%40lti_consumer%2Bblock%408338de93e7c3499688734a1469b4eca9) , if anyone have an idea please help me, thank you.
The nulls at the first line are happening because you have 2 repeated conditional statements if (i == 0)
and the other nulls on each line are happening because you are concatenating the char with the string using +=
which is giving nullA etc so instead do this:
String[][] teamView = new String[24][35];
for (int i = 0; i < teamView.length; i++)
{
for (int j = 0; j < teamView[i].length; j++)
{
if (j == 0 && i == 0)
{
teamView[i][j] = "@";
} else if (j == 0)
{
teamView[i][j] = String.valueOf((char) (i + 64)) ; // the nulls on each line fix
} else if (j == 1)
{
teamView[i][j] = " ";
} else if (i == 0 && j % 3 == 0) //instead of the only i == 0 check its a multiple of 3
{
teamView[i][j] = String.valueOf(j / 3); //instead of the switch you can just divide the j by 3 if it's is a multiple of 3
} else if (i == 0)
{
teamView[i][j] = " ";
} else
teamView[i][j] = "#";
System.out.print(teamView[i][j]);
}
System.out.print("\n");
}
}
Output
@ 1 2 3 4 5 6 7 8 9 10 11
A #################################
B #################################
C #################################
D #################################
E #################################
F #################################
G #################################
H #################################
I #################################
J #################################
K #################################
L #################################
M #################################
N #################################
O #################################
P #################################
Q #################################
R #################################
S #################################
T #################################
U #################################
V #################################
W #################################