-1

I write a calculator and want to get the Text of a JTextField and split it into two Variables (number1 and number2). That won´t work as i get everytime a 0.0 as a Result. How can I get that to work? Here you see the Minus Operation:

else if (action.equals("-")) {
    String calc = TextField.getText();
    String lookfornumber = calc.substring(calc.lastIndexOf("-") + 1);
    double zahl2 = Double.parseDouble(lookfornumber);
    String looknumberbefore = calc.substring(calc.lastIndexOf("-") + 2);
    double zahl1 = Double.parseDouble(looknumberbefore);
    erg = zahl1 - zahl2;
    answer = String.format("%.1f - %.1f = %.2f", zahl1, zahl2, erg);
    TextField.setText(answer);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You're asking about problems with code that doesn't work, meaning you'll want to create and post a decent [mre] with your question. Please read the link. – Hovercraft Full Of Eels Sep 04 '22 at 18:48
  • Also, when you debug your code and your problem, are you testing that the JTextfield has the text that you want when you are trying to extract this information? Print out the text field text to console in that code above to see what it is holding at that time. – Hovercraft Full Of Eels Sep 04 '22 at 18:50
  • The reason that I mention the above, is because quite often the problem is due to trying to extract and manipulate the text *before* the user has had a chance to enter it. Be sure that you are doing all of the above within an event-listener for this very reason. – Hovercraft Full Of Eels Sep 04 '22 at 18:51
  • I corrected the Code in StackOverflow so you have a working Example. When I print the variable "erg, zahl1 and zahl2" to the Console I get "0.0" for erg, "5,0" for zahl1 and "5,0" for zahl2. what I typed in was 15 - 5. – AlexanderR Sep 04 '22 at 18:55
  • Your substrings look to be getting the same sub sections of the String. Fix that. Look up `String#substring(...)` to see what you're getting and why. – Hovercraft Full Of Eels Sep 04 '22 at 19:07

1 Answers1

1

This code:

String lookfornumber = calc.substring(calc.lastIndexOf("-") + 1);
double zahl2 = Double.parseDouble(lookfornumber);
String looknumberbefore = calc.substring(calc.lastIndexOf("-") + 2);
double zahl1 = Double.parseDouble(looknumberbefore);

is extracting the same information twice -- the number after the negative sign.

Instead, you want something like:

String lookfornumber = calc.substring(calc.lastIndexOf("-") + 1).trim();
double zahl2 = Double.parseDouble(lookfornumber);
String looknumberbefore = calc.substring(0, calc.lastIndexOf("-")).trim();
double zahl1 = Double.parseDouble(looknumberbefore);

two get both sides of the expression.

Or, you could split the String:

String[] tokens = calc.trim().split("\s*-\s*");
double myZahl1 = Double.parseDouble(tokens[0]);
double myZahl2 = Double.parseDouble(tokens[1]);

Or you could use equation parsing techniques as described here: How to evaluate a math expression given in string form?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373