'Mutiple iterators in for loop Java not working properly
I am trying to pass a Javascript code to Java, but I've got a problem with the for loop, I have a for with 2 iterators in my Javascript code, and it is working as intended, but on Java it is not, am I missing something?
I let you just the for loop structure, as I am pretty sure the remaining logic is working good.
It is a large chunk of code, but the problem on Java is that after the loop finishes it does not continue with the remaining code, as you can see the System.out.println("Not running from here, after the for loop is completed"); it just does not appear on console, It just stops after the loop is done. Am I mising something?
This is my code in Javascript:
function addTraceData(s1, s2, trace) {
var rows = trace.length - 1;
var cols = trace[0].length - 1;
var s1aligned = [];
var s2aligned = [];
var best, bestX, bestY, curX, curY;
var strings = ['', ''];
for (curX = 1, curY = 1; curX <= cols && curY <= rows;) {
console.log(curX, curY)
best = -1;
bestX = curX;
bestY = curY;
for (var y = curY; y <= rows; y++) {
if (trace[y][0] == trace[0][curX] && trace[y][curX] > best) {
best = trace[y][curX];
bestX = curX;
bestY = y;
}
}
for (var x = curX; x <= cols; x++) {
if (trace[curY][0] == trace[0][x] && trace[curY][x] > best) {
best = trace[curY][x];
bestX = x;
bestY = curY;
}
}
if (best >= 0) {
// trace[bestY][bestX] = '<span class="bg-danger"><b> ' + trace[bestY][bestX] + ' </b></span>';
var diffX = bestX - curX;
var diffY = bestY - curY;
strings[0] += repeat('-', diffY);
strings[1] += repeat('-', diffX);
for (var i = 0; i < diffX && curX + i <= cols; i++) {
strings[0] += trace[0][curX + i];
// strings[1] += trace[curX + i][0];
}
for (var i = 0; i < diffY && curY + i <= rows; i++) {
strings[1] += trace[curY + i][0];
// strings[0] += trace[0][curY + i];
}
strings[0] += trace[0][bestX];
strings[1] += trace[bestY][0];
} else {
strings[0] += '-';
strings[1] += '-';
}
curX = bestX + 1;
curY = bestY + 1;
}
for (var i = curX; i <= cols; i++) {
strings[0] += trace[0][i];
strings[1] += '-';
}
for (var i = curY; i <= rows; i++) {
strings[1] += trace[i][0];
strings[0] += '-';
}
return strings;
}
This is my code in Java:
public static void generarAlineamientoMejorado(String s1, String s2, String[][] trace) {
int rows = trace.length - 1;
int cols = trace[0].length - 1;
int best, bestX, bestY;
var curX = 1;
var curY = 1;
String firstString = "";
String secondString = "";
for (curX = 1, curY = 1; curX <= cols && curY <= rows;) {
System.out.println(curX + " " + curY);
best = -1;
bestX = curX;
bestY = curY;
for (int y = curY; y <= rows; y++) {
if (trace[y][0].equals(trace[0][curX]) && Integer.parseInt(trace[y][curX]) > best) {
best = Integer.parseInt(trace[y][curX]);
bestX = curX;
bestY = y;
}
}
for (int x = curX; x <= cols; x++) {
if (trace[curY][0].equals(trace[0][x]) && Integer.parseInt(trace[curY][x]) > best) {
best = Integer.parseInt(trace[curY][x]);
bestX = x;
bestY = curY;
}
}
if (best >= 0) {
// trace[bestY][bestX] = '<span class="bg-danger"><b> ' +
// trace[bestY][bestX] + ' </b></span>';
int diffX = bestX - curX;
int diffY = bestY - curY;
firstString += repeat("-", diffY);
secondString += repeat("-", diffX);
for (int i = 0; i < diffX && curX + i <= cols; i++) {
firstString += trace[0][curX + i];
// secondString += trace[curX + i][0];
}
for (int i = 0; i < diffY && curY + i <= rows; i++) {
secondString += trace[curY + i][0];
// firstString += trace[0][curY + i];
}
firstString += trace[0][bestX];
secondString += trace[bestY][0];
} else {
firstString += "-";
secondString += "-";
}
curX = bestX + 1;
curY = bestY + 1;
}
System.out.println("Not running from here, after the for loop is completed");
for (var i = curX; i <= cols; i++) {
firstString += trace[0][i];
secondString += '-';
}
for (var i = curY; i <= rows; i++) {
secondString += trace[i][0];
firstString += '-';
}
}
I have been searching through the internet and I found a post with the similar structure, but it is not working as intended, what am I missing?
Thank you in advance, sorry if my english is not that good.
Solution 1:[1]
Syntactically i am not seeing any issue with your code,
but are you sure you need to increment curX and curY in java code, you haven't incremented it in js code.
for (curX = 1, curY = 1; curX <= cols && curY <= rows; ) {}
also have you declared variable in java before that?
for (int curX = 1, curY = 1; curX <= cols && curY <= rows; ) {}
question is unclear, but hope this should work
according to comment, i can see that you need the opposite, but it should work just fine if you add ++ in js
> for (curX = 1, curY = 1; curX <= cols && curY <= rows;curX++,curY++){console.log("hello")}
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
undefined
i ran this in nodejs without any issue
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |