0

I have made a Java Interpreter, in which the user may type in operations like the following:

$x+23/$y+9*$z

$x , $y, $z are variables previously defined.

With another piece of code, I will replace those variables with their respective values and then I want to put the string back together.

Example:

$x = 1 $y = 2 $z = 3

So the operation will finally look like this:

1+23/2+9*3

How do I efficiently extract those variables from the main String and fit them back in, given the circumstances that the user might perform operations of different length and order?

3
  • You could use regular expressions to search for Strings consisting of $ followed by an arbitrary number or characters to get all variables - store them and after that iterate over them to perform String#replace with each variable and its respective value. Commented Jun 1, 2016 at 9:39
  • @Smutje I'm still new to programming so I'm struggling to understand you. Sorry D: Commented Jun 1, 2016 at 12:38
  • @nicomp I tried to extract substring between operators, but it somehow ended up in a mess and I lost the overview. I looked at multiple posts regarding 'substring out of strings', but none of them could give me an understandable explanation. Commented Jun 1, 2016 at 12:40

1 Answer 1

2

I think you should parse your input string completely to separate it into meaningful tokens, and you should not rely on e.g. search-and-replace.

Search-and-replace does not take the whole context into account, so even if there are syntax errors, or multi-character variable names - it will blindly replace part of user input data. E.g. consider that your user has defined a variable named 'e' and then he writes a number in exponential format like: 1e-2 (for 0.01). Obviously the 'e' in the number should not be treated as the above mentioned variable 'e' in this context.

So to make it orderly: define for yourself what your tokens are. It seems to me they are:

  1. Floating point numbers (an integral number can be considered a special case of it, if you do not want to consider the type of user-provided expressions). Think if you want to accept exponential format for numbers as well (e.g. 1e-2 for 0.01)
  2. Operators: +,*,/ (and perhaps others?)
  3. Variable names (starting with $, perhaps multicharacter? perhaps case-(in)sensitive?)

After defining the tokens, define your grammar. E.g. in the so called Polish Notation writing "2 3 +" is a representation for "2+3" (Polish Notation is easy for evaluating expressions using stack, it frees you from analyzing parentheses and different priorities of your operators).

Then parse user input to extract tokens and to analyze their relation according to your grammar. Otherwise you will get very many corner cases for which your program does not work well, including user typos that you will be unable to delimit (you should provide a 'syntax error' message in such cases.

Sign up to request clarification or add additional context in comments.

1 Comment

Variables always start with '$' and end at the next operator (+,-,*,/), so an exponential number won't be treated as a variable. Variables are case-sensitive. All that should be possible are standard calculations using the basic operators '+,-,*,/'. I have an Error handler which checks whether the input is correct or not (e.g. checking if the variable is valid). Unfortunately, I'm a newb at programming so your text is a little bit complicated to understand for me haha.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.