My input string looks like this "10^-9"
. Since Java don't handle exponentiation and "^"
stands for bitwise XOR, I tried to do it differently. I splitted the String in 10 and -9 and parsed it into double, then used Math.pow()
to get the value of it. The code looks like this:
String[] relFactorA = vA.getValue().split("^"); //vA.getValue is a String like `"10^-9"` or any other number instead of 9.
Double pow1 = Double.parseDouble(relFactorA[0]);
Double pow2 = Double.parseDouble(relFactorA[1]);
Double relFactor = Math.pow(pow1, pow2);
System.out.println(relFactor);
But that approach results in an java.lang.NumberFormatException. In the code I cannot find an error, whether I did something wrong or the compiler recognizes the "-(Minus)" as a "-(hyphen)", but I dont think thats the reason, because both Strings look the same and the compiler should see this.
You probably didn't split on ^
since split
uses regex as parameter and in regex ^
has special meaning (its start of line). To make it literal use "\\\\^"
.
String.split()
uses a regex as parameter. If you want to split for the symbol ^
use \\\\^
instead.
String[] relFactorA = vA.getValue().split("\\^");
Note that String#split
takes a regex and not a String:
public String[] split(String regex )
You should escape the special character ^
:
vA.getValue().split("\\\\^");
Escaping a regex is usually done by \\
, but in Java, \\
is represented as \\\\
.
Instead, you can use Pattern#quote
that will treat the ^
as the String ^
and not the meta-character ^
:
Returns a literal pattern String for the specified String.
vA.getValue().split(Patter.quote("^"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.