Question: Write a program that asks the user to enter a series of numbers separated by commas. Here are two examples of valid input: 7,9,10,2,18,6 8.777,10.9,11,2.2,-18,6
Write a program that asks the user to enter a series of numbers separated by commas. Here are two examples of valid input: 7,9,10,2,18,6 8.777,10.9,11,2.2,-18,6 The program should calculate and display the sum of all the numbers. Input Validation 1. Make sure the string only contains numbers and commas (valid symbols are '0'-'9', '-', '.', and ','). 2. If the string is empty, a messages should be displayed, saying that the string is empty.
Here's my code so far.
The problem I am having is I dont get how to implement the EXIT_SENTINENTAL into the program and how to do the input validation for the numbers and comma oh and also
tell if the string is empty to display the string is empty message
import java.util.Scanner;
import java.io.*;
public class sum_digits {
public static void main(String[] args) {
// TODO Auto-generated method stub
String inputstring;
double sum = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a series of numbers separated only by commas or type QUIT to exit:");
inputstring = keyboard.nextLine();
String[] tokens = inputstring.split(",");
for (String str:tokens)
sum += Double.parseDouble(str);
System.out.println("The sum of those numbers is " + sum);
keyboard.close();
}
}
Thanks for the help!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
