Ad
Code
Diff
  • public class StringCalculator{
      
      public static int add(String numbers){
       int result = 0;
        
        if(numbers == "")
          return result;      
       
       for(String n: numbers.split(","))
       {
          result = result + Integer.parseInt(n);  
       }
       return result;
      }
      
    }
    • public class StringCalculator{
    • public static int add(String numbers){
    • if(numbers == "") {
    • return 0;
    • }
    • int result = 0;
    • String[] numbersArr = numbers.split(",");
    • if (numbersArr.length > 1) {
    • int result = Integer.parseInt(numbersArr[0]) + Integer.parseInt(numbersArr[1]);
    • return result;
    • }
    • return Integer.parseInt(numbers);
    • if(numbers == "")
    • return result;
    • for(String n: numbers.split(","))
    • {
    • result = result + Integer.parseInt(n);
    • }
    • return result;
    • }
    • }