Question

Given a string containing space-separated integer values, compute the sum of those values. Each string will...

Given a string containing space-separated integer values, compute the sum of those values. Each string will contain at least one number.


sumString("1 2") → 3
sumString("1 2 3") → 6
sumString("7 -7 0 59") → 59

int sumString(String str) {
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class SumString {

    int sumString(String str) {
        int sum = 0;
        String[] words = str.split(" ");
        for (int i = 0; i < words.length; i++) {
            sum += Integer.parseInt(words[i].trim());
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(new SumString().sumString("1 2"));
        System.out.println(new SumString().sumString("1 2 3"));
        System.out.println(new SumString().sumString("7 -7 0 59"));
    }
}
Add a comment
Know the answer?
Add Answer to:
Given a string containing space-separated integer values, compute the sum of those values. Each string will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public static boolean isArithmetic(java.lang.String text) Given a string of text containing numbers separated by commas, returns...

    public static boolean isArithmetic(java.lang.String text) Given a string of text containing numbers separated by commas, returns true if the numbers form an arithmetic sequence (a sequence in which each value differs from the previous one by a fixed amount). For example, given "2,4,6,8", the method returns true given "-2,5,12,19,26", returns true given "2,4,7", returns false given "1,2,23skidoo", returns false The method should return true for any string containing two or fewer numbers and false for any invalid string. Assume that...

  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

  • Given two int values, return their sum. Unless the two values are the same, then return...

    Given two int values, return their sum. Unless the two values are the same, then return double their sum sum_double (1, 2) 3 sum_double (3, 2) 5 sum_double (2, 2)8 def sum_double(a, b) Python Warmup-2 > front_times prev next chance Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Retun n copies of the front Expected...

  • A perfect number is a positive integer that is equal to the sum of its (proper)...

    A perfect number is a positive integer that is equal to the sum of its (proper) positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number evenly (i.e., without a remainder). For example, consider number 6. Its divisors are 1, 2, 3, and 6. Since we do not include number itself, we only have 1, 2, and 3. Because the sum of these divisors of 6 is 6, i.e., 1 + 2...

  • This is for a java program public class Calculation {    public static void main(String[] args)...

    This is for a java program public class Calculation {    public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object    //Task 1. Write your code here //Print the prompt and ask the user to enter an...

  • Please help me with those 2 question problem, please help, thanks!! Code (Please use visual studio):...

    Please help me with those 2 question problem, please help, thanks!! Code (Please use visual studio): #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Your job is to complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify...

  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • 2. This question is about processing strings 2.1 [4 marks] Given a string str, we can...

    2. This question is about processing strings 2.1 [4 marks] Given a string str, we can calculate a checksum of the string by summing up the ASCII codes of the characters in the string. For example, the checksum of a string "apple" is 530, which equals to 'a' + 'p' + 'p' + 'l' + 'e' = 97 + 112 + 112 + 108 + 101. Write a function int calculate_checksum(char str[] that calculates and returns the checksum of the...

  • Java problem In this problem, the first input is a positive integer called n that will...

    Java problem In this problem, the first input is a positive integer called n that will represent the number of lines to process. The lines to be processed have one or more integers separated by whitespaces. For each of these lines, you must output: The minimum value of the integers The maximum value of the integers The sum of the integers It is worth to mention that the number of integers of each line is not known a priori and...

  • Short Answer Given the following class declaration for a binary string/integer pair (the binary n...

    ANSWER IN C++ Constructors Short Answer Given the following class declaration for a binary string/integer pair (the binary number, in the form of a string. should be the binary form of the integer): class binint public: int num; // Note this is an address! string *bin; // Note that this is an address! binint (int n); binint (string s); void convertitos(int n); void convertstoi(string s); II NOTE: you do not have to do anything // with this method you can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT