0

i must divide the results of two strings in Java. I hava tried valueOF, parseUnsignedInt and other methods, but the problem is, that for the first string they does not work. The strings are getText() results from a certain web element, which i am getting using Selenium. Here is my code

System.out.println( Integer.valueOf(st) /Integer.parseUnsignedInt( element.getAttribute("innerHTML").substring(16) ));

Here is error which i have

Exception in thread "main" java.lang.NumberFormatException: For input string: "10,768"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:652)
    at java.base/java.lang.Integer.valueOf(Integer.java:983)
    at com.company.Main.main(Main.java:64)

10768 is the string number for my String st How can I handle this situation to get the division number correctly?

1 Answers1

1

You can convert the String value containing the comma into an integer using:

int value = NumberFormat.getNumberInstance(java.util.Locale.US).parse(element.getAttribute("innerHTML").substring(16));

And then you can use this value for your division like:

System.out.println(Integer.valueOf(st)/value);
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20