-1

Below is the code I have written

List<Integer> ar = new ArrayList<Integer>();
// Below loop for converting string to int and adding to list.
for (int in = 0; in < count; in++) {
        String str = ((JavascriptExecutor) driver)                                           
        .executeScript("return arguments[0].innerHTML;", AllCardPrice.get(in)).toString().trim();
        String str1 = str.replace("$", "").replace(",", "").trim();
        System.out.println("Amount on card " + AllCardPrice.get(in).getText());
        if (!str1.equals("")) {
           int priceoncard = Integer.parseInt(str1);
           ar.add(priceoncard);
        }
   }

I am getting following console error:

java.lang.NumberFormatException: For input string: "7500000
                        <!---->"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
Nithin
  • 1
  • 1

1 Answers1

0

You could do string sanitization to remove all non-digit characters from the input string, as well as put a try-catch block around the code so that each input is handled separately :)

List<Integer> ar = new ArrayList<Integer>();
for (int in = 0; in < count; in++) {
        String str = ((JavascriptExecutor) driver)                                           
        .executeScript("return arguments[0].innerHTML;", AllCardPrice.get(in)).toString().trim();
        //remove any NON-numeric values: 
        String str1 = str.replaceAll("[^\\d]", "");
        System.out.println("Amount on card " + AllCardPrice.get(in).getText());
        //Add a try-catch block:
        try{
            if (!str1.equals("")) {
               int priceoncard = Integer.parseInt(str1);
               ar.add(priceoncard);
            }
        } catch(Exception e) {
            System.out.println("Oh no, caught an exception!");
        }
   }

Non-numeric code removal reference: Java String remove all non numeric characters but keep the decimal separator

ferrouskid
  • 466
  • 2
  • 7