Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

What should I do when I receive the error - exception in thread main java lang numberformatexception for input string

Asked by John Ingerson Oct 11, 2022 1.3K views 1 answer
Share

About this question

Scenario: I am using Selenium Java. I need to add items to the cart and calculate the total price of the items in the cart.
Software Quality Assurance & Testing
Error: Exception in thread "main" java.lang.NumberFormatException: For input string: ""
Asked 1 year, 11 months ago
Modified 1 year, 11 months ago
Viewed 914 times
0
Scenario: I am using Selenium Java. I need to add items to the cart and calculate the total price of the items in the cart.
Url: https://rahulshettyacademy.com/seleniumPractise/#/
enter image description here
Issue: My code is giving the following error
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
My code:
String[ ] ItemsNeeded = {"Cucumber","Brocolli","Beetroot" };
addItemsCart (driver,ItemsNeeded);


public static void addItemsCart(WebDriver driver, String [] ItemsNeeded) {
List products = driver.findElements(By.cssSelector("h4.product-name"));
List ItemsNeededList = java.util.Arrays.asList(ItemsNeeded); 
int j = 0;
int CalcTotalPrice = 0;
for (int i=0;i
{
String[] name2 = products.get(i).getText().split("-");
String name3 = name2[0].trim(); 
   if (ItemsNeededList.contains(name3)) 
  {                 
      driver.findElements(By.xpath("//div[@class='product-action']/button")).get(i).click();
System.out.println("Added " + name3 + " to the cart");
String Price1 =driver.findElements(By.xpath("//p[@class='product-price']")).get(i).getText();
int IndPrice1 = Integer.parseInt(Price1);
CalcTotalPrice = CalcTotalPrice + IndPrice1 ;
System.out.println ("Calculated Total Price " +CalcTotalPrice);                 
j++;
       if (j==ItemsNeededList.size())
      {
       break;
      }
    }
}

Can anyone help me in resolving this issue?

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.