In Chapter 8, you wrote an application named DistanceFromAverage that allows a user to enter up to 15 double values and then displays each entered value and its distance from the average. Now, modify that program to first prompt the user to enter an integer that represents the array size. Java generates a NumberFormatException if you attempt to enter a noninteger value using nextInt(); handle this exception by displaying an appropriate error message.
Create an array using the integer entered as the size. Java generates a NegativeArraySizeException if you attempt to create an array with a negative size; handle this exception by setting the array size to a default value of five. If the array is created successfully, use exception-handling techniques to ensure that each entered array value is a double before the program calculates each element’s distance from the average.
===================================================================
import java.util.*;
public class DistanceFromAverageWithExceptionHandling
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
double[] numbers = new double[15];
double entry;
double total = 0;
double average = 0;
final int QUIT = 99999;
int x = 0, y;
System.out.print("Enter a numeric value or " +
QUIT + " to quit >> ");
entry = input.nextDouble();
while(entry != QUIT && x < numbers.length)
{
numbers[x] = entry;
total += numbers[x];
++x;
if(x < numbers.length)
{
System.out.print("Enter next numeric value or " +
QUIT + " to quit >> ");
entry = input.nextDouble();
}
}
if(x == 0)
System.out.println("Average cannot be computed because no numbers
were entered");
else
{
average = total / x;
System.out.println("You entered " + x +
" numbers and their average is " + average);
for(y = 0; y < x; ++y)
System.out.println(numbers[y] + " is " +
(numbers[y] - average) + " away from the average");
}
}
}
If you need any corrections/clarifications kindly comment
Program
import java.util.*;
public class DistanceFromAverageWithExceptionHandling
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String val;
double entry=0;
double total = 0;
double average = 0;
final int QUIT = 99999;
int x = 0, y,size=0,count=0;
double[] numbers = new double[5];
boolean isValOk = false;
boolean success = false;
while (!success) {
try {
System.out.print("Enter array size: ");
val=input.next();
size=Integer.parseInt(val);
success = true;
} catch (NumberFormatException e) {
System.out.println("You have entered invalid data");
}
}
try {
numbers = new double[size];
} catch (NegativeArraySizeException ex) {
System.out.println("Can't create array of negative size!!!So
setting the array size to a default value of five.");
size=5;
}
while(entry != QUIT && x < numbers.length)
{
if(x < numbers.length)
{
try
{
System.out.print("Enter next numeric value or " +QUIT + " to quit
>> ");
entry = input.nextDouble();
numbers[x] = entry;
total += numbers[x];
++x;
}
catch(InputMismatchException e)
{
System.out.println("Entered array value should be a double");
input.nextLine();
}
}
}
if(x == 0)
System.out.println("Average cannot be computed because no numbers
were entered");
else
{
average = total / x;
System.out.println("You entered " + x +
" numbers and their average is " + average);
for(y = 0; y < x; ++y)
System.out.println(numbers[y] + " is " +
(numbers[y] - average) + " away from the average");
}
}
}
Output
Enter array size: -2
Can't create array of negative size!!!So setting the array size to
a default value of five.
Enter next numeric value or 99999 to quit >> 1
Enter next numeric value or 99999 to quit >> 2
Enter next numeric value or 99999 to quit >> 3
Enter next numeric value or 99999 to quit >> word
Entered array value should be a double
Enter next numeric value or 99999 to quit >> 4
Enter next numeric value or 99999 to quit >> 5
You entered 5 numbers and their average is 3.0
1.0 is -2.0 away from the average
2.0 is -1.0 away from the average
3.0 is 0.0 away from the average
4.0 is 1.0 away from the average
5.0 is 2.0 away from the average
Get Answers For Free
Most questions answered within 1 hours.