I have a function that takes String and a char[] type variables as parameter.
public String compressLine(String line, char[] fileChars){
String outputLine = new String();
int count = 0;
char lastChar = fileChars[0];
for(char c : line.toCharArray()){
if(c!=lastChar){
outputLine += String.format("%o,", count);
lastChar = c;
count = 1;
}else{
count += 1;
}
}
outputLine += count;
return outputLine;
}
String line is a line of ascii art and the char[] fileChars consists of the characters that are used in the string mentioned beforehand.
Example of line:
"@@@@@@@@@@---"
Example of fileChars:
[-, @]
Algorithm works in the following manner: given a row of data, the algorithm replaces each row with numbers that say how many consecutive pixels are the same character, always starting with the number of occurrences of the character that is encountered first in the fileChars.
When I ran a test using the examples given above, It should return;
[0, 10, 3]
But the output I get is;
[0, 12, 3]
When I tried to solve the bug, I saw the integer count somehow hops from 7 to 10.
What is causing this?