subject

Need 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 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 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 tt

import java. util.*;
public class
{
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 = ;
boolean isarraynegative = false;
boolean cancreatearray = true;
boolean isvalok = false;
int x = 0, y;

try
{
system. out. print(" 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");
}
}
}
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 19:20
Amedian in the road will be marked with a white sign that has a black arrow going to the left of the median. true false
Answers: 1
question
Computers and Technology, 23.06.2019 16:10
What is the ooh? a. omaha occupation handbook b. online occupational c. occupations online d. occupational outlook handbook select the best answer from the choices provided
Answers: 3
question
Computers and Technology, 24.06.2019 07:00
Why would a business likely use a java applet - to back up their data files for the business - to create a program that a customer can launch in their web browser - to create music on a powerpoint presentation - to organize files on their company directory
Answers: 3
question
Computers and Technology, 24.06.2019 12:10
What is it called during the editing process when the processor ensures that a character holding a coffee mug from one angle is holding the same mug in the same way when the shot switches to another camera at another angle? cinematography continuity technology prop use
Answers: 1
You know the right answer?
Need correcting my java code for exception handling!
the prompt for the exercise is rather...
Questions
question
Mathematics, 17.04.2021 18:00
question
Mathematics, 17.04.2021 18:00
question
Mathematics, 17.04.2021 18:00
Questions on the website: 13722360