1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Gnom [1K]
3 years ago
6

Write a program that asks the user to enter a series of numbers separated by commas. Here is an example of valid input: 7,9,10,2

,18,6 The program should calculate and display the sum of all the numbers.

Computers and Technology
1 answer:
alex41 [277]3 years ago
8 0

Answer:

Here is the JAVA program. Let me know if you need the program in some other programming language.

import java.util.Scanner; // used for taking input from user

public class Main{ //Main class

public static void main(String[] args) {//start of main() function body

  Scanner scan = new Scanner(System.in); // creates a Scanner type object

   String input; // stores input series

    int sum = 0; //stores the sum of the series

    System.out.println("Enter a series of numbers separated by commas: "); //prompts user to enter a series of numbers separated by comma

     input = scan.nextLine(); //reads entire input series

     String[] numbers = input.split("[, ]"); //breaks the input series based on delimiter i.e. comma and stores the split sub strings (numbers) into the numbers array

   for (int i = 0; i < numbers.length; i++) { //loops through each element of numbers array until the length of the numbers reaches

          sum += Integer.parseInt(numbers[i]); } // parses the each String element of numbers array as a signed decimal integer object and takes the sum of all the integer objects

     System.out.println("Sum of all the numbers: " + sum);  } } //displays the sum of all the numbers

Explanation:

The program prompts the user to enter a series separated by commas.

Then split() method is used to split or break the series of numbers which are in String form, into sub strings based on comma (,) . This means the series is split into separate numbers. These are stored in numbers[] array.

Next the for loop iterate through each sub string i.e. each number of the series, converts each String type decimal number to integer using Integer.parseInt and computes the sum of all these integers. The last print statement displays the sum of all numbers in series.  

If the input is 7,9,10,2,18,6

Then the output is: 7+9+10+2+18+6

sum = 52

The program and its output is attached.

You might be interested in
What are the first letters of each of the 3 main colors in javaScript? (seperate with commas)
Oxana [17]

Answer:

R, G, B

Explanation:

Red, Green, Blue

4 0
3 years ago
Write a subclass called SavingsAccount that extends Account and adds an interest rate variable. Write a constructor with 3 argum
AveGali [126]
Okayama sintered Nuni 19371
5 0
3 years ago
A plain text e-mail and the same text entered into a word processing document would be about the same file sizes.
kumpel [21]
The email should be lighter but they're variables such as the word processing program. Not sure if this answered your question.
6 0
4 years ago
Read 2 more answers
What are the pros and cons of using unique closing reserved words on compound statements?
Nata [24]

Pros: Readability. Unique closing reserved words on compound statements will enhance readability and flexibility of a language. When an endwhile or an endif in a program is written by someone else, it is vivid which block is ending. Using braces is much harder is sometimes much harder to read.

Cons: Writability. They have the disadvantage of writability and thus, complicating the language by increasing the number of keywords. Use of more of these reserved words mean less words available for the programmer.







6 0
4 years ago
What does the following expression evaluate to?: "2 + 8"
lozanna [386]
10 tell me if I’m wrong
7 0
3 years ago
Other questions:
  • I need help please <br> just plug in the words with their definitions.......
    10·2 answers
  • Which programming component provides a temporary, named storage location in computer memory that cannot change during program ex
    6·1 answer
  • What type of USB connector is always plugged into the computer to connect in external device
    14·1 answer
  • What type of network component is Telnet?
    10·2 answers
  • Why don’t we need to know and memorize the IP addresses for our favorite websites?
    15·1 answer
  • "Why learning how to type is so important.
    7·1 answer
  • Me Completan Pliiiis
    11·1 answer
  • Dgvdsgf cvdzgb fgvsdxchygfdrzvdszfgvsdzxd
    9·1 answer
  • By what other name can the folders in Windows 7 be called?
    5·1 answer
  • It is not really important to organize your photos because you know that they are on your computer. (TRUE/FALSE)
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!