Need help correcting my Java code for Exception handling!
The prompt for the exercise is rather long and a little confusing for me.

So I don't know if I can get this 100% right.

QUESTION IN HAND: 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.

So when I try it out, it looks okay, but then it says that my NegativeArraySizeException is not working and has one other error I cannot seem to figure out. My logic would be a lot better if I did this in pseudocode but I'm honestly hopeless... TT


import java.util.*;
public class DistanceFromAverageWithExceptionHandling
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
double[] numbers;
double entry = 0;
int enteredSize = 0;
double total = 0;
double average = 0;
final int QUIT = 99999;
boolean isArrayNegative = false;
boolean canCreateArray = true;
boolean isValOk = false;
int x = 0, y;

try
{
System.out.print("Please enter a value for the array size:");
enteredSize = input.nextInt();
}
catch(Exception e)
{
System.out.println("Invalid value for array size");
canCreateArray = false;
input.nextLine();
}

if(enteredSize < 0)
{
System.out.print("Array cannot be negative. Automatically value of size 5");
while(!isArrayNegative)
{
enteredSize = 5;
input.nextLine();
}
}

if(canCreateArray)
{
numbers = new double[enteredSize];
while(!isValOk)
{
try
{
System.out.print("Enter a numeric value or " + QUIT + " to quit >> ");
entry = input.nextDouble();
isValOk = true;
}
catch(Exception e)
{
isValOk = false;
input.nextLine();
}
}
while(entry != QUIT && x < numbers.length)
{
numbers[x] = entry;
++x;
if(x < numbers.length)
{
try
{
System.out.print("Enter next numeric value or " +
QUIT + " to quit >> ");
entry = input.nextDouble();
}
catch(Exception e)
{
--x;
input.nextLine();
}
}
}
if(x == 0)
System.out.println("Average cannot be computed because no numbers were entered");
else
{
for(int a = 0; a < numbers.length; ++a)
total += numbers[a];
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 " +
(average - numbers[y]) + " away from the average");
}
}
}
}

Respuesta :

umm i dont think anyone knows the answer cause its too long mate.


Answer:

The code below should help your issue with exception handling. Modify as appropriate

Explanation:

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