Regular Expression for Simple Maths Equation parser in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently learning Java, and trying to parse a maths equation for valid inputs:
For example, the user has to input integers in the form:
Operand Operator Operand
in that specific order, and the program would then need to be able to tell if the inputs were in that form, and then work out the simple equation.
An example would be:
4 * 8 which the result would give as 32
the program would also reject something like 45.6 * 0.3, or 45 + 3 / 4
For this to work, do I have to use regular expression, or some other method of if loops?

You can use RegEx to on one hand, get rid of the invalid input, and on the other hand make use of the groups to extract the operands and operation and apply the math
/^(\\d+)\\s*([+\\-*\\/])\\s*(\\d+)$/
Edit:
As Rory Daulton pointed out, the signed integers are excluded from the above RegEx, so the below one should be used instead
/^([+\\-]?\\d+)\\s*([+\\-*\\/])\\s*([+\\-]?\\d+)$/

Take look that:
https://stackoverflow.com/a/11009403/8701820
^([-+/*]\d+(\.\d+)?)*
Also you can check this website: https://regex101.com

Related

Is there a way to use python and java in the same program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to use both python and java in the same program. Since the print() function of python is better, but java's int variable; is more efficient.
If I'm interpreting correctly, you want to use to use both interchangeably in the same file, so you'd end up with code like:
def main():
int x = 5;
print(x)
This is impossible, because there would be ambiguity when trying to interpret code if you allowed constructs from both languages. For example, "X" + 1 is allowed in java, and would give you the string "X1". In python, it would give you an error because you can't add an int to a string. This would mean that there would be no way to know what your code should do because it's runnable in both languages.
This is a problem that all of us face, where we like some parts of some languages and other parts of other languages. The solution is pretty much just to decide what's most important, choose one language based on that, and then put up with the parts you don't like.
You can use Jython, which is a Python implementation based on the JVM/JDK. This allows calling between Java and Python code in both directions.

Regex best practice: multiple patterns or single one with combined expression? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Think of the following scenario:
Application receives a list of regex from server (HTTP GET returns a List with each item indicating a regular expression.);
User input text needs to be validate against these expressions;
Application runs on Android, so memory is an issue;
List of expressions is not frequently changed.
What would be better:
Cache several Pattern objects, each one containing a single regex returned from server;
Concatenate the regex - (REGEX1)|(REGEX2)|(REGEX3)...|(REGEXN) - and maintain a single object on memory? - refreshing it whenever a single regex is added or removed, which doesn't happens very often.
I don't imagine there is a way to answer this question without having a specific list of regex's and the list of input. Because, each regex/input combination is going to result in a different amount of memory used. Here is what my instincts tell me:
Evaluate the Regex's one at a time. In the "OR" scenario, the regex must simultaneously evaluate all of the OR'd expressions, so that would take more RAM, I believe.
Order the Regex's in order of either: (a) Likelihood to match, so that you can abandon evaluating the rest of the regex's or (b) Early non-matching, so that the regex can be quickly discarded as never going to match (for example "^a" only requires evaluating the first character of the string where as "a" requires searching the whole string for an "a".)
Ultimately, only testing can really tell you what takes more time/memory, I'm afraid.

Take a result of two long numbers using an array in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm

Java Arbitrary Expression Evaluator? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Write the Java code for an arbitrary expression evaluator (supporting _, -, *, /). The - and / operator only works on two operands, the others can have any number or operands. The / operator will additionally check that there is no 0 in the second operand. If it does, then it will throw a BadArithmeticException.
Write the code using the Composite pattern. Also write a client class that will create objects and calculate expressions to demonstrate the use of the composite pattern. The common method in the composite hierarchy is called eval. Here is the signature for eval.
public int eval() throws BadArithmeticException { ...
Not really sure where to begin here. Any help would be appreciated.
This isn't really a question but I can offer a suggestion at least. I would start by trying to write your exception so that you understand what you should be doing. You can find this in the Oracle docs here:
http://docs.oracle.com/javase/tutorial/essential/exceptions/creating.html
If you know that you can't divide by 0, think about what kinds of expressions a user (or you) could input that would cause a BadArithmeticException.

Modular Arithmetic in programming [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anyone explain me how modular arithmetic works in programming? I know it is used to operate on large values.
For example, to calculate the binomial coefficient of B(1000000, 2) using int data-type. i assume we couldn't multiply using int data-type, since it involves calculating factorials of big values like 1000000! which has millions of digits, which don't fit in an 32-bit or 64-bit integer.
I know modular arithmetic is used to these type of problems, But i don't understand exactly how that works.
The modulo operation is a simple operation that calculates the remainder of a division.
For instance
5 % 3 = 2 as dividing 5 by 3 will give you a a remainder of .
A common usecase for this is checking whether a number is even or odd.
number % 2 == 0 means the number is even.
For more information please check Wikipedia.

Categories

Resources