Search code examples
javabooleanchars

Stuck with boolean always returning TRUE


hello i'm new at the java world and i stuck in my code and it would be really nice a litle hel p

im having a problem with a bolean at this code/

public class Variables {
  boolean bit;
  String name;
  public Variables(int b, String name){
    this.name = name;
    bit = test(b);
  }
  public boolean test(int b) {
    System.out.println(b);
    if(b==49) {
        return true;
    }
    if(b==48) {
        return false;
    }
    else {
        return false;
    }        
  }
}

the problem is whatever the number of b it keeps returning true actualy what im trying is to get a number 1 or 0 witch is in a chars and transforme in a boolean t/f

thanks in advance

MORE CODE

public class truthtable2 {
  public ArrayList <Variables[]>bits  = new ArrayList<>();

  public truthtable2 (ArrayList <String> inputs){       
    String [] inputsTab = inputs.toArray(new String[inputs.size()]);
    Variables[] bittab = new Variables[inputsTab.length];
    int total = (int) (Math.pow(2,inputs.size()))-1;
    String tab[]=new String[total+1];
    for(int i =0;i<=total;i++){
      tab[i]=(String.format("%16s", Integer.toBinaryString(i)).replace(' ', '0'));      
    }
    for(int i = 0;i<tab.length;i++){
      char[] chars = tab[i].toCharArray();

      for(int x = 0;x<inputs.size();x++){
        int d = 15-x;
        bittab[x]= new Variables(chars[d], inputsTab[x]);           
      }
      bits.add(bittab);  
    }
    for(Variables[] d: bits){
      for(int f = 0;f<d.length;f++){
        System.out.format("%4s %4s \n",d[f].bit,d[f].name);
      }
    }
  }
}

EDIT

'0' -->A false
'0' -->B false
'0' -->Cin false
'1' -->A true
'0' -->B false
'0' -->Cin false
'0' -->A false
'1' -->B true
'0' -->Cin false
'1' -->A true
'1' -->B true
'0' -->Cin false
'0' -->A false
'0' -->B false
'1' -->Cin true
'1' -->A true
'0' -->B false
'1' -->Cin true
'0' -->A false
'1' -->B true
'1' -->Cin true
'1' -->A true
'1' -->B true
'1' -->Cin true

EDIT 2

at this execution

for(Variables[] d: bits){
          for(int f = 0;f<d.length;f++){
            System.out.format("%4s %4s \n",d[f].bit,d[f].name);

i have this result

true    A 
true    B 
true  Cin 
true    A 
true    B 
true  Cin 
true    A 
true    B 
true  Cin 
true    A 
true    B 
true  Cin 
true    A 
true    B 
true  Cin 
true    A 
true    B 
true  Cin 
true    A 
true    B 
true  Cin 
true    A 
true    B 
true  Cin 

Solution

  • You have at least two major problems and 1 minor problem.

    1. Your code is hardcoding the number of zeroes in your binary string. Then, when you try to parse it, you have all these extra zeroes for no reason.
    2. This is the one that makes all the trues You keep reusing the same Variables[] array bittab, so each time it gets overwritten. You need to generate a new one.
    3. Your test function is overly complicated. I fixed it in the below code.

    Here is the fixed code:

    public class truthtable2 {
      public ArrayList<Variables[]> bits  = new ArrayList<>();
    
      public truthtable2(ArrayList <String> inputs){       
        String [] inputsTab = inputs.toArray(new String[inputs.size()]);
        int total = (int) (Math.pow(2,inputs.size()))-1;
        String tab[]=new String[total+1];
        for(int i =0;i<=total;i++){
          // changed the below line
          tab[i]=(String.format("%"+inputs.size()+"s", Integer.toBinaryString(i)).replace(' ', '0'));
          System.out.println(tab[i]);
        }
        for(int i = 0;i<tab.length;i++){
          char[] chars = tab[i].toCharArray();
    
          Variables[] bittab = new Variables[inputsTab.length]; // Moved this here
          for(int x = 0;x<inputs.size();x++){
            // Changed this to use variable size
            int d = inputs.size()-x-1;
            bittab[x]= new Variables(chars[d], inputsTab[x]);           
          }
          bits.add(bittab);  
        }
        for(Variables[] d: bits){
          for(int f = 0;f<d.length;f++){
            System.out.format("%4s %4s \n",d[f].bit,d[f].name);
          }
        }
      }
    
      public class Variables {
        boolean bit;
        String name;
        public Variables(int b, String name){
          this.name = name;
          bit = test(b);
        }
        // Totally rewrote function
        public boolean test(int b) {
          System.out.println(b);
          return b==49;
        }
      }
    }