-1

I want to code a simple calculator and already got some code. But now I want to change the String I got there into an Operator. For example:

My input is: "1,5 - 1,1 + 3,2 =" Now I have a double array and a String array. So after that I want to put it together, so it calculates this complete task.

double result = double[0] String[0] double[1] ....

I hope you can help there, and I apologize for my grammar etc., english is not my main language.

import java.util.*;
public class calculator 
{

       public static void main(String[] args) 
       {
             int a = 0;
             int b = 0;
             double[] zahl;
             zahl = new double[10];
             double ergebnis;
             String[] zeichen;
             zeichen = new String[10];

             Scanner input = new Scanner (System.in);

             while (input.hasNext())
             {
                    if (input.hasNextDouble())
                    {
                           zahl[a] = input.nextDouble();
                           a++;
                    }
                    else if (input.hasNext())
                    {
                           zeichen[b] = input.next();
                           if (zeichen.equals ("=")) break;
                           b++;
                    }

             }
             input.close();




       }

}

If I type in: "1,5 + 2,3 * 4,2 =" I want to get the result with point before line and without .math

TreffnonX
  • 2,924
  • 15
  • 23
Kanubbel
  • 27
  • 5
  • As far as I know, you can't do that. You may use a `switch-case` statement, and depending on the symbol given by the user, do an addition, or a substraction, ... – vincrichaud Apr 08 '19 at 08:47
  • Well, what did you try so far? There should be a lot of tutorials on this. Due to operator precedence you can't just parse the strings and execute the commands so you'll probably have to build something like an abstract syntax tree. Here's some more info on how to parse such expressions: http://www.sunshine2k.de/coding/java/SimpleParser/SimpleParser.html – Thomas Apr 08 '19 at 08:47
  • i am new to java, so i dont have the knowledge to get the information so easy. I dont understand most of it. – Kanubbel Apr 08 '19 at 08:49
  • It's not an easy task to just get a result out of string. What can help You with parsing and calculating the result is [Reverse Polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation) – zolv Apr 08 '19 at 08:50
  • Well, if you're new to Java (and I suspect to programming in general) then I'd suggest starting with easier tasks and acquire some knowledge first. – Thomas Apr 08 '19 at 08:53
  • Okay, i will try something else, i didn't know that this is so complicated. Thank you anyway :D – Kanubbel Apr 08 '19 at 08:58

1 Answers1

1

What you want to do is parse a single String and convert it into a mathematical expression, which you then want to resolve and output the result. For that, you need to define a "language" and effectively write an interpreter. This is not trivial, specifically if you want to expand your syntax with bracketing and thelike.

The primary question you have to answer is, whether you want to use a solution (because you are not the first person to attempt this) or if you want to actually write this yourself. There are "simple" solutions, for example, you could instantiate a javascript engine in Java and input your string, but that would allow much more, and maybe even things you don't want. Or you could use a library which already does this. This Thread already answered a similar question with multiple interesting answers: How to evaluate a math expression given in string form?

Otherwise, you might be in for a surprise, concerning the amount of work, you are getting yourself into. :)

TreffnonX
  • 2,924
  • 15
  • 23