Question

In: Computer Science

Design and implement a program that reads a series of 10 integers from the user and...

Design and implement a program that reads a series of 10 integers from the user and prints their average. Read each input value as a string, and then attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException (meaning that the input is not a valid number), print an appropriate error message and prompt for the number again. Continue reading values until 10 valid integers have been entered.

Solutions

Expert Solution

Code

public class Main
{
   public static void main(String[] args) {
  
   int sum = 0;
   double average;
   int i = 0;   
  
   // Reading 10 integers as strings and adding
   // into sum after conversion from string to int
  
   while(i< 10)
   {
   String value = System.console().readLine();
   try
   {
   sum = sum + Integer.parseInt(value);
   }
   catch(Exception NumberFormatException )
   {
   System.out.println("Please enter the valid number\n");
   continue; // Again asking for number so skipping increment of i
   }
   i++;
   }
  
   average = (double)sum/10;
       System.out.printf("average is: %f", average);
   }
}

Sample Output


Related Solutions

USING C# Design and implement a program (name it SumValue) that reads three integers (say X,...
USING C# Design and implement a program (name it SumValue) that reads three integers (say X, Y, and Z) and prints out their values on separate lines with proper labels, followed by their average with proper label. Comment your code properly.Format the outputs following the sample runs below. Sample run 1: X = 7 Y = 8 Z = 10 Average = 8.333333333333334
programing language JAVA: Design and implement an application that reads a sentence from the user, then...
programing language JAVA: Design and implement an application that reads a sentence from the user, then counts all the vowels(a, e, i, o, u) in the entire sentence, and prints the number of vowels in the sentence. vowels may be upercase
*C++* /* This program reads a movie rating from 1 to 10 from the user and...
*C++* /* This program reads a movie rating from 1 to 10 from the user and prints a corresponding word for the rating. Programmer: Your name here Date:       Current date here */ #include #include using namespace std; int main() {       int rating; // user entered rating       cout << "Enter your movie rating (an integer from 1 to 10): ";       cin >> rating;       cout << "\nThe movie was ";       // Select the appropriate word for the...
Write a main function that reads a list of integers from a user, adds to an...
Write a main function that reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation. Please do this with C++
1. Write an assembly language program that prompts the user for and reads four integers (x1,...
1. Write an assembly language program that prompts the user for and reads four integers (x1, y1, x2, y2) which represent the coordinates of two points. Make sure you keep track of which number is which. 2. Treat the line between the points as the radius of a sphere and compute the surface area of the sphere. Print the output with a label, such as “The surface area of the sphere is: …”. Hint: The distance between the points is...
Use DevC++ to implement a program that can store and output 5 integers, where the user...
Use DevC++ to implement a program that can store and output 5 integers, where the user can input its value into an array named arrayValue. The program should implement the following: 1. Calculate the sum and average of the array. 2. Output the content (value) that stored in the array of five elements. 3. Continue to ask the user to store for storing data into another array. c++ ONLY PLEASE NEED IT FAST AS POSSIBLE
Design and implement a C++ program that performs the following steps:Ask the user to enter a...
Design and implement a C++ program that performs the following steps:Ask the user to enter a positive integer number N; Your program may need to prompt the user to enter many times until it reads in a positive number;Let user to enter N (obtained in the previous step) floating point numbers, and count how many positive ones there are in the sequence and sum up these positive numbers; (Hint: negative numbers or 0 are ignored).Display result.You can and should use...
Python 10 - (10 pts) - Implement a program that starts by asking the user to...
Python 10 - (10 pts) - Implement a program that starts by asking the user to enter a userID (i.e., a string). The program then checks whether the id entered by the user is in the list of valid users. The current user list is: ['joe', 'sue', jamal, 'sophie'] Depending on the outcome, an appropriate message should be printed. Regardless of the outcome, your function should print 'Done.' before terminating. Here is an example of a successful login: >>> Login:...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads in the principle 2) reads in additional money deposited each year (treat this as a constant) 3) reads in years to grow, and 4) reads in interest rate And then finally prints out how much money they would have each year. See below for formatting. Enter the principle: XX Enter the annual addition: XX Enter the number of years to grow: XX Enter the...
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT