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
This is the simplest type. A bool expresses a truth value. It can be either 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.
If the specified boolean value is true, it returns Boolean. TRUE or if it is false, then this method returns Boolean. FALSE.
Press Ctrl+Shift+R and then choose Invert Boolean.
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?
You have at least two major problems and 1 minor problem.
Variables[]
array bittab
, so each time it gets overwritten. You need to generate a new one.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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With