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, 21.06.2019 23:00
Explain briefly why you cannot expect to find a previous version of every file with which you work.
Answers: 1
question
Computers and Technology, 22.06.2019 22:40
In this lab, you complete a python program that calculates an employee's annual bonus. input is an employee's first name, last name, salary, and numeric performance rating. if the rating is 1, 2, or 3, the bonus rate used is .25, .15, or .1 respectively. if the rating is 4 or higher, the rate is 0. the employee bonus is calculated by multiplying the bonus rate by the annual salary.
Answers: 1
question
Computers and Technology, 23.06.2019 04:31
Which of the following is not a way in which trees benefit the environment? a. they remove a significant amount of carbon dioxide from the atmosphere. b. they remove a significant amount of oxygen from the atmosphere. c. their roots hold soil in place, reducing rates of erosion. d. they remove ozone and particulates from the atmosphere. select the best answer from the choices provided a b c d
Answers: 1
question
Computers and Technology, 23.06.2019 13:30
Best laptops for college [$100-$500 range]?
Answers: 2
You know the right answer?
Need correcting my java code for exception handling!
the prompt for the exercise is rather...
Questions
question
Mathematics, 06.11.2019 02:31
question
Mathematics, 06.11.2019 02:31
question
Mathematics, 06.11.2019 02:31
question
Mathematics, 06.11.2019 02:31
question
Biology, 06.11.2019 02:31
Questions on the website: 13722361