How to make a Boolean algebra truth table from user input [closed]

I’m trying to write Java code to prompt the user to enter a Boolean algebra expression and display the truth table of that expression. I am very lost and I’ve done tons of research, but I haven’t found any useful information on how to approach it.

I will attach my code but it’s extremely messy. I’m trying to write it under the method DisplayTruthTable

/*LAB ASSIGNMENT 2: BOOLEAN ALGEBRA AND TRUTH TABLES
 * ANA MAVAREZ
 * 
 * */
import java.util.*;

public class a2 {
    static Scanner sc = new Scanner(System.in);
    public static void main(String [] args){
                
        do {
            
            int varCount= getTotalVariables();
            
            System.out.println(" you entered: " + varCount);
            
            String exp = getExpression(varCount);
            exp = exp.replaceAll("and", "&&").replaceAll("or", "||").replaceAll("not", "!");
            System.out.println("input is : " + exp);
            displayHeader(varCount);
            
            System.out.println("Repeat program? Y or N");
            String answer = sc.next();
            if (!answer.equals("Y")) {
                break;
            }
            
        }while(true);
        
    
        //num of combinations with the variables.
        /*
         *  int combinations = (int) Math.pow(2, varCount);
         System.out.println(combinations);
         
         for (int i = 0; i < varCount; i++) {
                System.out.print("Var" + (i + 1) + "\t");
            }
            System.out.println("Result");
            
         * 
         * */
        
            
            
    }
    public static int getTotalVariables() {
        System.out.println("Enter total count of variables (between 1 and 4): ");
        //int input = sc.nextInt();
        String input = sc.nextLine();
        
        try {
            int number = Integer.parseInt(input);
            if (number >= 1 && number <= 4) {
                return number;
            }else {
                System.out.println("Invalid number! please enter variable count between 1 and 4 /n");
                return getTotalVariables();
            }
            
        } catch(NumberFormatException e ) {
            System.out.println("INVALID!!! Input has to be a number between 1 and 4 \n\n");
            return getTotalVariables();
        }
        
    
        
        
    }
    public static String getExpression(int var) {
        /*Pass in total count of variables and prompt user to enter expression and
return expression. Validate the use of variables in the expression. For 1
variable, use x. For 2 variables, use x,y. For 3 variables, use x,y,z. For 4
variables use x,y,z,w. Use not, and, or boolean operators in the
expression
         * */
        String expression = "";
        if(var == 1) {
            System.out.println("Enter a boolean expression using variable name(s) 'x'"
                    + "and using 'and', 'nor', OR 'not'");
            return sc.nextLine().toLowerCase();
        }else if( var == 2 ) {
            System.out.println("Enter the expression using variable name(s) x,y :"
                    + "and using 'and', 'nor', OR 'not'");
            return sc.nextLine().toLowerCase();
        }else if(var ==3){
            System.out.println("Enter the expression using variable name(s) x,y,z :"
                    + "and using 'and', 'nor', OR 'not'");
            return sc.nextLine().toLowerCase();
        }else {
            System.out.println("Enter the expression using variable name(s) x,y,z,w :"
                    + "and using 'and', 'nor', OR 'not'");
            return sc.nextLine().toLowerCase();
        }
    }
    public static void displayHeader(int varNum) {
        //Display header information, the heading of the truth table.
        //ex: variable names
        switch(varNum) {
        case 1: 
            System.out.printf("x%n");
            System.out.printf("============%n");
            break;
        case 2:
            System.out.printf("x y%n ");
            System.out.printf("================%n");
            break;
        case 3:
            System.out.printf("x y z%n ");
            System.out.printf("==================%n");
            break;
        case 4:
            System.out.printf("x y z w%n ");
            System.out.printf("=====================%n");
            break;
        }

        
        }
    
    public static void DisplayTruthTable(int varNum, String expression) {
        //Display truth table with all variables and output columns
        for(int i = 0; i<= varNum; i++ ) {
            
        }

    }

    }

I prompted the user to enter the number of variables and enter an expression. I’m struggling how to convert that expression into something the code can interpret and convert into a truth table.

  • Does this help answer your question? stackoverflow.com/questions/3422673/…

    – 

  • I would suggest writing a parser that creates an AST (en.wikipedia.org) of the boolean expression. From there, we can extract all variables and all value combinations.

    – 

Leave a Comment