I need help with my code. It has a runtime error where the loop just stops midway... I made some checkers and I saw it only looped until the outer loop's middle part. I made a "stopper" which changes in size everytime a term is added to the arraylist so the new term will then be checked again <- is this the cause? Should the stopper be constant at all times?
Anyhow, please give your suggestions on how I should improve my code. Code is:
public void mainOperation() {
for(int i = 0; i<mainRec.size(); i++) {
int marker = 0;
int sizeC = mainRec.size();
for(int k = i+1; k<sizeC; k++) {
String var1 = elementBinValue(mainRec.get(i));
String var2 = elementBinValue(mainRec.get(k));
if(compareTo(var1, var2)==1) {
marker++;
mainRec.add(mainRec.get(i) + " " + mainRec.get(k));
System.out.println("CHECKER: " + mainRec.get(i) + "-" + mainRec.get(k));
}
}
if(marker==0)
markedP.add(mainRec.get(i));
}
}
EDIT: For easier visualisation, I'll add elementBinValue() and compareTo() methods here
public String elementBinValue(String input) { //gets the bin value of the whole element
StringTokenizer element = new StringTokenizer(input);
String firstElement = toBinary(Integer.parseInt(element.nextToken()));
if(element.countTokens()==0) { //RETURNS the whole elements' String value (1 String)
return firstElement;
}
else {
String newElement = "";
while(element.hasMoreTokens()) {
String compBin = toBinary(Integer.parseInt(element.nextToken()));
int iterator = 0;
while(iterator<firstElement.length()) {
if(firstElement.charAt(iterator)==compBin.charAt(iterator))
newElement += firstElement.charAt(iterator);
else
newElement += "-";
}
}
return newElement;
}
}
public int compareTo(String a, String b) {//returns 1 if they can be grouped else 0
int counter = 0;
int iterator = 0;
//String newElement = "0";
while(counter<2 && iterator < 8) {//The number 8 is for the binary size
if((a.charAt(iterator))!=((b.charAt(iterator))))
counter++;
iterator++;
}
if(counter<2)
return 1;
else return 0;
}