I am trying to convert userinput's each characters to logical expression and trying to print the Boolean result. but I always get result as false.
Lets say user enters : (( (a|~B) & (C&D) ) & E & ( ~A | a | D&G ))
my program convert this to an logical expression as written below and returns Boolean result.
(((false|false) & (true&true)) & true & (false | false|true&true))
But the result always shows false. When i write
System.out.println(true|true);
The output is True. But when my code returns the same expression it gives false.
Here is the code :
public class logicoperator {
boolean b;
static String str;
public boolean logicresult (String logexpression) {
str = logexpression;
str = str.replaceAll("[ABCDEFG]", "t");
str = str.replaceAll("[abcdefg]","f");
str = str.replace("~t", "f");
str = str.replace("~f", "t");
String str1 = str.toString();
//System.out.println(str1);
String op = "";
char[] ch = str1.toCharArray();
for(int i = 0; i< ch.length; i++) {
if(ch[i] == 't') { op=op+"true";}
else if(ch[i] == 'f') {op=op+"false";}
else if(ch[i] == '(') {op=op+"(";}
else if(ch[i] == '&') {op=op+"&";}
else if(ch[i] == '|') {op=op+"|";}
else if(ch[i] == ')') {op=op+")";}
else if(ch[i] == '~') {op=op+"~";}
}
return (Boolean.valueOf(op));
}
}