Ludvig Balázs Ludvig Balázs - 20 days ago 5
Java Question

Splitting a String discards characters

I'm trying to make my own calculator, and when I enter a fractional number ,and try to use the split method to split the string into two pieces (eg: 2.3+4.7) instead of splitting it into 2.3 and 4.7, it splits it into 2 and 7.
It doesnt matter if I use a dot or a coma, the result is always the same.

String[] input = Num.split("[+-/%*]");
double num1 = Double.parseDouble(input[0]);
double num2 = Double.parseDouble(input[1]);


Please help, I have no clue how to solve this.

Answer

Some of those characters need to be escaped in regex, specifically +, - and *:

String[] input = iNum.split("[\\+\\-/%\\*]");