Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please explain how to count the digits in a string?? For example: 1

ID: 3627475 • Letter: C

Question

Can someone please explain how to count the digits in a string??
For example:
1 2 3 4 = 10

I have this homework problem in which it has already set up part of the code and I just need to fill in the rest and I know I need to use Integer.parseInt(); I just dont know where:

import java.io.*;
import java.util.*;

public class EchoSum extends Echo
{

public EchoSum (String datafile) throws IOException
{
super(datafile);
}

// Prints the sum of each line.
public void processLine(String line){
//my code goes here!

Explanation / Answer

{
        int sum=0;

        for(int i=0;i<line.length();i++)

            {

                sum = sum + Integer.parseInt(line.substring(i, i+1));

            }

    System.out.println(sum);