6

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 
  • 9
    Passing anything except 49 should result in `false`. Show more code. –  Nov 28 '12 at 14:09
  • i know because this i have the System.out.println(b); to check out here is the output >48 48 48 49 48 48 48 49 48 49 49 48 48 48 49 49 48 49 48 49 49 49 49 49 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 the number changes but not the boolean result – willian ver valem Nov 28 '12 at 14:10
  • this totally should work. perhaps your printing of the result asks the same object? try out print(b); pring(test(b)); – schippi Nov 28 '12 at 14:14
  • 2
    post the code of method test. The code is absolutely fine. – Raghunandan Nov 28 '12 at 14:14
  • 2
    Post the code where you are testing the value of "bit" and the code for your output "...49 true A true B true Cin". What is printing the "true". That's probably the culprit. The code you currently have for the method "test" is fine. – km1 Nov 28 '12 at 14:17
  • 1
    Add a `toString()` to `Variables` and call it right after `bittab[x]= new Variables(chars[d], inputsTab[x]); ` and also print `chars[d]` at that point. – jlordo Nov 28 '12 at 14:22
  • 1
    Am I the only one really struggling to figure out this code? – lynks Nov 28 '12 at 14:23
  • @user1841450: Can you provide the input strings also? – durron597 Nov 28 '12 at 14:29
  • yes but will make no cense once it pass via a filereader before – willian ver valem Nov 28 '12 at 14:40
  • @user1841450: I fixed it. See my answer – durron597 Nov 28 '12 at 15:01
  • thanks a lot guys fixed how do i close this thread – willian ver valem Nov 28 '12 at 15:17

3 Answers3

3

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?

lynks
  • 5,293
  • 6
  • 21
  • 42
2

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;
    }
  }
}
durron597
  • 30,764
  • 16
  • 92
  • 150
1

EDIT

change

System.out.format("%4s %4s \n",d[f].bit,d[f].name);

to

System.out.format("%5s %4s \n",d[f].bit,d[f].name);

because you are trying to output a boolean. true has 4 characters, but false has 5 :)

OLD:

why not

public boolean testChar(char c) {
    return c == '1';
}

or

public boolean testInt(int c) {
    return c == 49;
}

EDIT

Add this code inside Variable class.

@Override
public String toString() {
    return name + " " + bit;
}

And then insert the following code after bittab[x]= new Variables(chars[d], inputsTab[x]);

System.out.println("'" + chars[d] + "' --> " + bittab[x].toString());

and show us the output.

jlordo
  • 35,188
  • 6
  • 52
  • 77