Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 824
willian ver valem Avatar asked Nov 28 '12 14:11

willian ver valem


People also ask

Is Boolean always true?

This is the simplest type. A bool expresses a truth value. It can be either true or false .

Does Boolean return true or false?

A Boolean expression is a Java expression that returns a Boolean value: true or false . This is useful when we want to compare values to find answers.

What does Boolean true return?

If the specified boolean value is true, it returns Boolean. TRUE or if it is false, then this method returns Boolean. FALSE.

How do you reverse a Boolean?

Press Ctrl+Shift+R and then choose Invert Boolean.


2 Answers

As far as I can see, there is nothing wrong with your code. How are you testing the value of bit? Can we see the wrapping code that instantiates Variables and calls the test() method?

like image 62
lynks Avatar answered Sep 30 '22 01:09

lynks


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;
    }
  }
}
like image 33
durron597 Avatar answered Sep 30 '22 02:09

durron597