Question
public class Main { public static void main(String[] args) { String [] a = { 3 cats?, 17, , 20 thousand! }; System.out.println( sumAll( a
public class Main { public static void main(String[] args) { String [] a = { "3 cats?", "17", "", "20 thousand!" }; System.out.println( sumAll( a ) ); // 13 } public static int sumAll( String[] a ) {
} public static int sumOne( String str ) {
} }
public static int sumOne( String str )
{
Return the sum of any digits in the string
For example, if s is "42 cats", return 6
If there are no digits, then it returns 0
Here's one way to check if a String of length 1 is a digit.
String x = a single character;
if ( x.compareTo("0") >= 0 && x.compareTo("9") <= 0 )
then x is "0", "1", "2", "3", "4", "5", "6", "7", "8", or "9"
Then you can safely use Integer.parseInt to convert the String to an int.
}
public static int sumAll( String[] a )
{
Call the sumOne method for each string in the array
Return the sum of all the returned values
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started