Computers and Technology

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");
}
}
}
}

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 17:30, jonquil201
Where would you click to edit the chart data?
Answers: 1
image
Computers and Technology, 23.06.2019 08:00, Buddy01
Match the items with their respective descriptions.
Answers: 1
image
Computers and Technology, 23.06.2019 14:00, shawn423
How are stop motion special effects in animated films created
Answers: 1
image
Computers and Technology, 24.06.2019 00:50, anthonycraig0205
3. what is the output of the following statements? temporary object1; temporary object2("rectangle", 8.5, 5); temporary object3("circle", 6, 0); temporary object4("cylinder", 6, 3.5); cout < < fixed < < showpoint < < setprecision(2); object1.print(); object2.print(); object3.print(); object4.print(); object1.set("sphere", 4.5, 0); object1.print();
Answers: 1
Do you know the correct answer?
Need correcting my java code for exception handling!
the prompt for the exercise is rather...

Questions in other subjects:

Konu
Spanish, 17.11.2020 08:00