0

I want to get value from web For eg : Total AMount : 25000 and want to convert this value into integer to proceed comparision step but after printing totat_Amt it displayed as

"java.lang.NumberFormatException: For input string: "25,000""

Here is my code :

WebElement gt = driver.findElement(By.id("totAmt"));
String total_Amt=gt.toString();
System.out.println("Total Amt:"+total_Amt);
//int total_amt_val =Integer.parseInt(total_Amt);
System.out.println(total_amt_val);
1
  • 2
    use String total_Amt=gt.getText(); and post the total_Amt output here. Also post the html fragment.
    – Abhilash
    Commented Jun 28, 2018 at 11:46

5 Answers 5

2

Try to replace all non-numeric symbols:

WebElement gt = driver.findElement(By.id("totAmt"));
String total_Amt = gt.toString(); // "25,000"

// replace 'bad' symbols
String onlyNumbers = total_Amt.replaceAll("[^\\d]", "");  // "25000"

System.out.println("Total Amt: " + total_Amt);
int total_amt_val = Integer.parseInt(onlyNumbers); // 25000
System.out.println(total_amt_val);

\\d means all numbers, [^\\d] means all non-numbers, and if you want to keep some another symbols, just add them into [] — e.g. use [\\d.] if you want to keep dots too.

2

You can use NumberFormat

NumberFormat.getNumberInstance(Locale.US).parse("25,000").intValue() will return 25000

1

You can try this:

WebElement gt = driver.findElement(By.id("totAmt"));
String total_Amt = gt.toString(); // 25,000
total_Amt = total_Amt.replaceAll(",", ""); // removes all ',' -> 25000
int total_amt_val = Integer.parseInt(total_Amt); // 25000 as int already
System.out.println(total_amt_val); // 25000
1

You can replace all the comma first using replaceAll method and then directly parse it as below. additionally, you need to use getText() method should be used to retrieve the element text.

WebElement gt = driver.findElement(By.id("totAmt"));
//To be changed as gt.getText().
String total_Amt=gt.getText();      
System.out.println("Total Amt:"+total_Amt);
//Replace comma as empty and then you can normal parse the string to int
int total_amt_val =Integer.parseInt(total_Amt.replaceAll(",",""));
System.out.println(total_amt_val)

In case , If you are getting the total_Amt value as Total AMount : 25000, then extract the amount value using substring method and then replace all the , as empty using replaceAll method

int total_amt_val =Integer.parseInt(total_Amt.substring(total_Amt.indexOf(":")+2).replaceAll(",",""));
1

If you are dealing with price, use this:

public static void main(String[] args) throws Exception {

       WebElement gt = driver.findElement(By.id("totAmt"));
       String total_Amt = gt.getText(); // total_Amt=25,000.00

       BigDecimal bd_amt = parse(total_Amt , Locale.US); // Use this if the value is price
       int int_amount = parse(total_Amt , Locale.US).intValueExact(); // Use this if you want integer

       System.out.println("Price : " + bd_amt);
       System.out.println("Amount : " + int_amount);
}

private static BigDecimal parse(final String amount, final Locale locale) throws ParseException {
    final NumberFormat format = NumberFormat.getNumberInstance(locale);
    if (format instanceof DecimalFormat) {
        ((DecimalFormat) format).setParseBigDecimal(true);
    }
    return (BigDecimal) format.parse(amount.replaceAll("[^\\d.,]", ""));
}

Sample Output:

 Price : 25000.00
 Amount : 25000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.