Search code examples
javainterpreternumberformatexceptioninstruction-set

My interpreter language doesn't seem to recognise the "+" symbol and is returning errors. How have I gone wrong in my code?


I have written an interpreter that takes a String as an instruction set. This instruction set declares a set of variables and returns one of them. An example instruction set would be:

A = 2
B = 8
C = A + B
C

This is supposed to return the variable c and print 10 in the console. The preconditions are that the left side must always be a letter, the right side can only either contain one figure or the addition of two figures, and the return statement must always be a variable.

I wrote the following code to do this:

public class Interpreter2 {

public static int Scanner() {
    int returnStatement = 0;
    String num1;
    String num2;
    int sum = 0;
    ArrayList<String> variables = new ArrayList<String>();
    ArrayList<Integer> equals = new ArrayList<Integer>();
    //Input instruction set in the quotation marks below
    String input = "A = 2\n B = 8\n C = A + B\n C";
    String[] lines = input.split("\n"); //Split the instruction set by line

    for (String i : lines) {
        i = i.replaceAll("\\s", "");
        if (i.contains("=")) {          //If the line has an equals, it is a variable declaration   
            String[] sides = i.split("="); //Split the variable declarations into variables and what they equal
            variables.add(sides[0]);

            if (sides[1].contains("\\+")) { //If the equals side has an addition, check if it is a number or a variable. Convert variables to numbers and add them up
                String[] add = sides[1].split("\\+"); 
                num1 = add[0];
                num2 = add[1];
                for (int j = 0; j < variables.size(); j++) {
                    if (variables.get(j).equals(num1)) {    
                        num1 = Integer.toString(equals.get(j));
                    }   
                } 
                for (int k = 0; k < variables.size(); k++) {
                    if (variables.get(k).equals(num2)) {    
                        num2 = Integer.toString(equals.get(k));
                    }
                }
                sum = Integer.parseInt(num1) + Integer.parseInt(num2);
                equals.add(sum);
            }else { //If the equals side has no addition, it is simply equals to the variable
                equals.add(Integer.parseInt(sides[1]));
            }

        }else { //If the line does not have an equals, it is a return statement
            for (int l = 0; l < variables.size(); l++) {
                if (variables.get(l).equals(i)) {   
                    returnStatement = equals.get(l);
                }
            }
        }

    }
     return returnStatement;
}


public static void main(String[] args) {
        System.out.println(Scanner());
    }
}

However this gives the error

Exception in thread "main" java.lang.NumberFormatException: For input string: "A+B"

This points to line 39:

else { 
     equals.add(Integer.parseInt(sides[1]));
     }

Which makes me think that my if statement which finds if the line has a "+" symbol in it is not working correctly. Can anyone see what I've missed out? I can explain any code if necessary, it's probably a huge mess.


Solution

  • The problem is that String#contains does not accept a regular expression. It accepts a CharSequence and:

    Returns true if and only if this string contains the specified sequence of char values.

    So you don't need to escape the +. Simply do:

    if (sides[1].contains("+"))