Meme Boi - 17 days ago 4
Java Question

# Numbers issue in for loops

I'm making a program that reads fences using terrain representation. Right now the code looks at the array and reports back how much fencing is needed, with each side of a 1 being 2.5m. The problem with it is that I can't figure out what's wrong with the numbers. It displays the corner values fine, but the side values are incorrect, except for the last column which displays the right values, but that might just be a coincidence. I have i's and j's to represent x and y values, here is all of my code so far.

``````package assignment_2;

public class Fencing {

public static void main(String[] args) {
boolean b = true;
double corner = 0;
double core = 0;
double side = 0;
int i = 0;
int j = 0;
int[][] map = {
{ 0, 1, 1, 0 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 0, 1 }
};
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (map[i][j] != 1 && map[i][j] != 0) {
b = false;
System.out.println("--> A value of " + map[i][j] + " was found at " + i + "," + j);
}
}
}
if (b == false && i == 4) {
System.out.println("The map is invalid");
return;
} else {
System.out.println("Map is valid");
}
if (map[0][0] == 1) {
corner += 5;
if (map[1][0] == 1 && map[0][1] == 1) {
corner += 0;
}
if (map[1][0] != 1 && map[0][1] != 1) {
corner += 5;
}
if (map[1][0] != 1 && map[0][1] == 1 || map[1][0] == 1 && map[0][1] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
if (map[0][3] == 1) {
corner += 5;
if (map[1][3] == 1 && map[0][2] == 1) {
corner += 0;
}
if (map[1][3] != 1 && map[0][2] != 1) {
corner += 5;
}
if (map[1][3] != 1 && map[0][2] == 1 || map[1][3] == 1 && map[0][2] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
if (map[3][0] == 1) {
corner += 5;
if (map[3][1] == 1 && map[2][0] == 1) {
corner += 0;
}
if (map[3][1] != 1 && map[02][0] != 1) {
corner += 5;
}
if (map[3][1] != 1 && map[2][0] == 1 || map[3][1] == 1 && map[2][0] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
if (map[3][3] == 1) {
corner += 5;
if (map[3][2] == 1 && map[2][3] == 1) {
corner += 0;
}
if (map[3][2] != 1 && map[2][3] != 1) {
corner += 5;
}
if (map[3][2] != 1 && map[2][3] == 1 || map[3][2] == 1 && map[2][3] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
System.out.println("Corner fencing = " + corner);
for (i = 1; i > 2; i++) { // Top
if (map[0][j] == 1) {
side += 2.5;
if (map[i + 1][0] == 1 && map[i - 1][0] == 1) {
side += 0;
}
if (map[i + 1][0] != 1 || map[i - 1][0] != 1) {
side += 2.5;
}
if (map[i + 1][0] != 1 && map[i - 1][0] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the first row, fencing = " + side);
for (i = 1; i > 2; i++) { // Bottom
if (map[i][3] == 1) {
side += 2.5;
if (map[i + 1][3] == 1 && map[i - 1][3] == 1) {
side += 0;
}
if (map[i + 1][3] != 1 || map[i - 1][3] != 1) {
side += 2.5;
}
if (map[i + 1][3] != 1 && map[i - 1][3] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the bottom row, fencing = " + side);
for (j = 1; j < 3; j++) {// Right
if (map[3][j] == 1) {
side += 2.5;
if (map[3][j + 1] == 1 && map[3][j - 1] == 1) {
side += 0;
}
if (map[3][j + 1] != 1 || map[3][j - 1] != 1) {
side += 2.5;
}
if (map[3][j + 1] != 1 && map[3][j - 1] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the first column, fencing = " + side);
for (j = 1; j < 3; j++) {// Left
if (map[0][j] == 1) {
side += 2.5;
if (map[0][j + 1] == 1 && map[0][j - 1] == 1) {
side += 0;
}
if (map[0][j + 1] != 1 || map[0][j - 1] != 1) {
side += 2.5;
}
if (map[0][j + 1] != 1 && map[0][j - 1] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the last column, fencing = " + side);
}
}
``````

The corners work properly, but the problem is the sides (the for loops). The sides run throught the current line of the array and if the number is a 1, it checks the numbers beside them and adds 2.5m if it is a 0. The issue looks like it's something to do with the i's and j's making the value incorrect but I'm not very sure what's wrong with them. Can anyone help me with this?

Here is the current output:

``````Corner fencing = 12.5

After the first row, fencing = 0.0

After the bottom row, fencing = 0.0

After the first column, fencing = 5.0

After the last column, fencing = 15.0
``````

And here is the expected output:

``````Corner fencing = 12.5

After the first row, fencing = 5.0

After the bottom row, fencing = 10.0

After the first column, fencing = 15.0

After the last column, fencing = 20.0
``````

I don't fully understand what a fence is in your code or what corners/sides represent but I will point out several problems that your code has, and where things might have gone wrong.

1- Shared local variables:

`int i,j;` are shared between several blocks so changing those in one block will definitely effect their state in another.

Sol: If the vars are meant to have the same name -> use named blocks

2- Wrong conditions: there are two wrong conditions in your code -bottom and top for loops-

``````(i = 1; i > 2; i++)
``````

Here is the modified code:

``````public class Fencing {

public static void main(String[] args) {
boolean b = true;
double corner = 0;
double core = 0;
double side = 0;

int[][] map = {
{ 0, 1, 1, 0 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 0, 1 }
};

firstBlock:{
int i = 0;
int j = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (map[i][j] != 1 && map[i][j] != 0) {
b = false;
System.out.println("--> A value of " + map[i][j] + " was found at " + i + "," + j);
}
}
}
if (b == false && i == 4) {
System.out.println("The map is invalid");
return;
} else {
System.out.println("Map is valid");
}
if (map[0][0] == 1) {
corner += 5;
if (map[1][0] == 1 && map[0][1] == 1) {
corner += 0;
}
if (map[1][0] != 1 && map[0][1] != 1) {
corner += 5;
}
if (map[1][0] != 1 && map[0][1] == 1 || map[1][0] == 1 && map[0][1] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
if (map[0][3] == 1) {
corner += 5;
if (map[1][3] == 1 && map[0][2] == 1) {
corner += 0;
}
if (map[1][3] != 1 && map[0][2] != 1) {
corner += 5;
}
if (map[1][3] != 1 && map[0][2] == 1 || map[1][3] == 1 && map[0][2] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
if (map[3][0] == 1) {
corner += 5;
if (map[3][1] == 1 && map[2][0] == 1) {
corner += 0;
}
if (map[3][1] != 1 && map[02][0] != 1) {
corner += 5;
}
if (map[3][1] != 1 && map[2][0] == 1 || map[3][1] == 1 && map[2][0] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
if (map[3][3] == 1) {
corner += 5;
if (map[3][2] == 1 && map[2][3] == 1) {
corner += 0;
}
if (map[3][2] != 1 && map[2][3] != 1) {
corner += 5;
}
if (map[3][2] != 1 && map[2][3] == 1 || map[3][2] == 1 && map[2][3] != 1) {
corner += 2.5;
}
} else {
corner += 0;
}
System.out.println("Corner fencing = " + corner);
}
secondBlock:{
int i = 0;
int j = 0;
for (i = 1; i < 2; i++) { // Top
if (map[0][j] == 1) {
side += 2.5;
if (map[i + 1][0] == 1 && map[i - 1][0] == 1) {
side += 0;
}
if (map[i + 1][0] != 1 || map[i - 1][0] != 1) {
side += 2.5;
}
if (map[i + 1][0] != 1 && map[i - 1][0] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the first row, fencing = " + side);}
thirdBlock:{
int i = 0;
int j = 0;
for (i = 1; i < 2; i++) { // Bottom
if (map[i][3] == 1) {
side += 2.5;
if (map[i + 1][3] == 1 && map[i - 1][3] == 1) {
side += 0;
}
if (map[i + 1][3] != 1 || map[i - 1][3] != 1) {
side += 2.5;
}
if (map[i + 1][3] != 1 && map[i - 1][3] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the bottom row, fencing = " + side);}
fourthBlock:{
int i = 0;
int j = 0;
for (j = 1; j < 3; j++) {// Right
if (map[3][j] == 1) {
side += 2.5;
if (map[3][j + 1] == 1 && map[3][j - 1] == 1) {
side += 0;
}
if (map[3][j + 1] != 1 || map[3][j - 1] != 1) {
side += 2.5;
}
if (map[3][j + 1] != 1 && map[3][j - 1] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the first column, fencing = " + side);}
lastBlock:{
int i = 0;
int j = 0;
for (j = 1; j < 3; j++) {// Left
if (map[0][j] == 1) {
side += 2.5;
if (map[0][j + 1] == 1 && map[0][j - 1] == 1) {
side += 0;
}
if (map[0][j + 1] != 1 || map[0][j - 1] != 1) {
side += 2.5;
}
if (map[0][j + 1] != 1 && map[0][j - 1] != 1) {
side += 5;
}
} else {
side += 0;
}
}
System.out.println("After the last column, fencing = " + side);}
}
}
``````

The output is:

``````Map is valid
Corner fencing = 12.5
After the first row, fencing = 0.0
After the bottom row, fencing = 5.0
After the first column, fencing = 10.0
After the last column, fencing = 20.0
``````

I've to point out that `first row fencing is equal to 0.0` not `5` and that's because of `(map[i + 1][0] != 1 && map[i - 1][0] != 1)` so you have to change this accordingly.

Source (Stackoverflow)