1

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;
}
Hiro'omi
  • 31
  • 5
  • Can you attach your runtime exception? – Tran Ho Oct 24 '18 at 01:45
  • there are no exceptions. The program just stops midway (I thought that was called runtime error). The ECLIPSE does not throw any error whatsoever – Hiro'omi Oct 24 '18 at 01:46
  • 3
    Perhaps you're swallowing exceptions somewhere? Hard to tell what your problem is and (at least for me) hard to analyze without a decent [mcve]. – Hovercraft Full Of Eels Oct 24 '18 at 01:48
  • what does `elementBinValue` do? – Scary Wombat Oct 24 '18 at 01:49
  • Also maybe change `mainRec.add(mainRec.get(i) + " " + mainRec.get(k));` so that it adds to a temporary list, and then add that list after the loops. – Scary Wombat Oct 24 '18 at 01:51
  • 1
    Did you determine that the loop stops midway JUST BY the `println`? Could there be chances that `compareTo(var1, var2)==1` does not satisfy at all after midway? Better add `println` outside the `if` block. – Ricky Mo Oct 24 '18 at 01:55
  • elementBinValue just returns the binary form in string – Hiro'omi Oct 24 '18 at 01:58
  • so should not continue to be using `var1` and `var2` e.g. `System.out.println("CHECKER: " + var1 + "-" + var2);` – Scary Wombat Oct 24 '18 at 02:02
  • If I use var1 or var2, the checker will return 00000100-00000101 instead of 4-5. elementBinValue is working fine. I think maybe modifying the stopper of the loop causes the error – Hiro'omi Oct 24 '18 at 02:11
  • Add `System.out.println("CHECKER: " + var1 + "-" + var2);` before `if(compareTo(var1, var2)==1) {` to see if it really stops. Maybe it is just not satisfying `compareTo(var1, var2)==1` – Ricky Mo Oct 24 '18 at 02:29
  • I did put println on all parts of the loop. 1 before and after the inner loop and 1 before the compareTo. I just didn’t them in this sample. It seems the println after the inner loop only executes twice. The println before the compareTo does execute fully. I need the outerloop to execute mainRec.size() times by which mainRec.size() gets bigger whenever compareTo adds a new element to the arraylist – Hiro'omi Oct 24 '18 at 02:35
  • If I understand correctly, you are adding `mainRec.get(i) + " " + mainRec.get(k)` to `mainRec`, where there is a space in between the two elements. For example, you may add `"4 5"` to `mainRec`. When the outer loop later come accross `"4 5"`, you try to call `elementBinValue("4 5")`. I guess `elementBinValue` involves parsing the string into number as it compute binary of the input number. However, `"4 5"` is not a number. Therefore, `compareTo(var1, var2)==1` never true after it get pass the initial `mainRec.size()` – Ricky Mo Oct 24 '18 at 02:39
  • Wait I’ll post all methods involving that part after class. elementBinValue parses the string and returns (0000010-) when the element (4 5) is passed. It can accept multiples of 2 such that the element (4 5 20 21) will return (000-010-). – Hiro'omi Oct 24 '18 at 03:34

0 Answers0