Reputation: 17
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);
Upvotes: 0
Views: 1086
Reputation: 445
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
Upvotes: 1
Reputation: 2334
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(",",""));
Upvotes: 1
Reputation: 5647
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
Upvotes: 1
Reputation: 15390
You can use NumberFormat
NumberFormat.getNumberInstance(Locale.US).parse("25,000").intValue()
will return 25000
Upvotes: 2
Reputation: 874
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.
Upvotes: 2