Java Logic Error

JC724

Weaksauce
Joined
Jan 20, 2016
Messages
118
I am writing a test program to add binary numbers and for some reason when I leave my loop my correct values get reset. Not sure why.

I print the results to the screen and in the loop it shows 1 1 in the output but out of the loop it is 1 0. So I am not sure why my array index 1 and beyond gets reset to zero.

Code:
public class testing {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String test = "110011";
        String test2  = "1010";
        int fNumL = 0;
        int sNumL = 0;
        int cBit = 0;
       
        calculations getResults = new calculations();
       
        //value = Integer.parseInt(test);
       

       
       
          int[] digits1 = new int[test.length()];

          for(int i = 0; i<test.length(); ++i){
            digits1[i] = Integer.parseInt(test.substring(i, i+1));
          }
         
          int[] digits2 = new int[test2.length()];

          for(int i = 0; i<test2.length(); ++i){
            digits2[i] = Integer.parseInt(test2.substring(i, i+1));
          }
         
          fNumL = digits1.length;
          sNumL = digits2.length;
         
          /*
          //Test cases
          System.out.println("Here is fNumL " + fNumL);
          System.out.println("Here is sNumL " + sNumL);
         
          System.out.println("Here are values from first digits1 array");
         
          for(int i = 0; i < digits1.length; i++) {
              System.out.print(digits1[i] + " ");
          }
          System.out.println("");
         
          System.out.println("Here are values from second digits2 array");
         
          for(int i = 0; i < digits2.length; i++) {
              System.out.print(digits2[i] + " ");
          }
          System.out.println("");
          */
         
         
         
          if(fNumL > sNumL) {
            int[] results = new int[fNumL];
            for (int j = (digits1.length - digits2.length)-1; j >= 0; j--) {
             
             
              for (int i = digits2.length-1; i >= 0; i--) {
                getResults.addValues(digits1[i],digits2[i],cBit);
                results[i] = getResults.results;
                cBit = getResults.carryB;
               
              }//end of for loop for smaller digit
              results[j] = digits1[j];
             
             
            //  System.out.println(digits1[j]);
              System.out.println(results[j]);
             
              System.out.println(" Here is J " + j);
             
            }//end of for loop for bigger digit
           
            System.out.println(results[0] + " " + results[1]);
           
           // getResults.showAnswer(results);
           

          }//end of if fNumL > sNumL
          else if(sNumL > fNumL){
            int[] results = new int[sNumL];
           
          }//end of else fNumL > sNumL
          else {
            int[] results = new int[sNumL];
          }
         
   
  }
         

}



class calculations {
    public int results;
    public int carryB;
   
   
    public void addValues(int value1, int value2, int cBit) {
       
        if(value1 == 1 && value2 == 0 && cBit == 0) {
            results = 1;
            carryB = 0;
           
        }else if(value1 == 0 && value2 == 1 && cBit == 0) {
            results = 1;
            carryB = 0;
        }else if(value1 == 1 && value2 ==1 && cBit == 0) {
            results = 0;
            carryB = 1;
        }else if(value1 == 1 && value2 == 0 && cBit == 1) {
            results = 0;
            carryB = 1;
        }else if(value1 == 0 && value2 == 1 && cBit == 1) {
            results = 0;
            carryB = 1;
        }else if(value1 == 0 && value2 == 0 && cBit == 0) {
            results = 0;
            carryB = 0;
        }else if(value1 == 1 && value2 == 1 && cBit == 1) {
            results = 1;
            carryB = 1;
        }
       
    }
   
    public void showAnswer(int[] results) {
        for(int i = 0; i < results.length; i++) {
            System.out.print(results[i]);
        }
    }
   
}
 

DrLobotomy

Supreme [H]ardness
Joined
May 19, 2016
Messages
6,736
public class testing {

public static void main(String[] args) {

You are missing a closing "}"
 
Top