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 23:30
What are some ways to use a range name in a formula? check all that apply. in the defined names group, click use in formula, and then select the desired name. begin typing the name in the formula, select a name from the autocomplete list, and use the arrow keys and tab key to enter the name in the formula. begin typing the formula, and then click and drag with the mouse to select the cells to include in the formula. right-click one of the cells in the range. click formula options, and use the dialog box to add the name.
Answers: 1
question
Computers and Technology, 23.06.2019 11:50
While preforming before operation pmcs, you notice the front right tire appears slightly under-inflated. what is the proper action?
Answers: 3
question
Computers and Technology, 24.06.2019 17:40
Anewly established internet company with 40 employees needs your advice. they are looking for a collaboration tool and have narrowed their choices to gotomeeting, webex, and my web conferences. after reading the information presented in this chapter and other sources, prepare a two- page document ( double spaced) that includes two advantages and two disadvantages of each tool. which one is your final recommendation? why did you choose that tool over the other two?
Answers: 3
question
Computers and Technology, 24.06.2019 19:50
Which is a characteristic of free and open-source software licenses? a. the source code is unavailable to everyone b. the source code is available only to the government c. the source code is available to all users d. the source code is constantly modified
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, 21.07.2021 14:00
question
Medicine, 21.07.2021 14:00
question
Mathematics, 21.07.2021 14:00
question
World Languages, 21.07.2021 14:00
Questions on the website: 13722361