0

I have problem.

  1. I create a list of the specified range of the keyboard.
  2. I create the map, which is a list of the key, and the values ​​are the result of the equation.

Here is my code:

static Map<Double, Double> podajDane(int lewy, int prawy) {
        //y=2x+3
        List<Double> listaX = new ArrayList();
        for (int i = lewy; i <= prawy; i++) {
            listaX.add((double) i);
        }
        Map<Double, Double> wynik = new HashMap<Double, Double>();
        for (Double liczba : listaX) {           
                double y = 2 * liczba + 3;  <---
                wynik.put(liczba, y);
        }
        return wynik;
    }
  1. I need to replace the line of code to the equation taken from the TextField entered by the user

    TextField field = new TextField();

  2. When the user enters the equation x + 2 should be downloaded from the list of value, which is the key maps.

How can i do this?

Giacomo
  • 341
  • 2
  • 8
  • 25
  • 1
    I think you are saying you want the user to be able to enter a formula in a text field, and then you want to apply that formula to a collection of values. Is that correct? That's a non-trivial task and the kind of thing you might be asked to outline at some length in a technical interview for a general entry-level software engineering job. There are some work arounds, by using another language that has dynamic evaluation (e.g. javascript): see e.g. http://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java – James_D Jan 14 '17 at 21:06
  • Hmm so how can i do this without map. I need the easiest way. – Giacomo Jan 14 '17 at 21:13
  • 1
    This simply isn't an easy task. Whether you store the data in a map or not is immaterial, really: the part that you are asking about is "how to convert the text in the text field to a function that is applied to numbers". You can send the text to a script engine (which is highly inefficient, and you would need to worry a lot about security), or build a parser yourself (a fun project, but way too broad for this forum), or find a third party library. I think the linked question answers this: if I have understood your question correctly this is essentially a duplicate. – James_D Jan 14 '17 at 21:17

0 Answers0