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 21:10
Dameas communication challenge is due to which factor
Answers: 2
question
Computers and Technology, 23.06.2019 12:30
Animations and transitions are added from the
Answers: 1
question
Computers and Technology, 24.06.2019 00:30
Which boolean operator enables you to exclude a search term? a} not b} and c} or d} plus
Answers: 1
question
Computers and Technology, 24.06.2019 00:30
The best definition of an idiom is a. a word or phrase that describes a noun b. a word or phrase describing a verb c. a phrase containing figurative language in which the word expresses a different idea from its exact meaning d. a phrase that compares two unlike objects or ideas
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.05.2020 04:16
question
Mathematics, 06.05.2020 04:16
question
Mathematics, 06.05.2020 04:16
question
Mathematics, 06.05.2020 04:16
question
Mathematics, 06.05.2020 04:16