Question

PLEASE LET ME KNOW WHERE I AM GOING WRONG IN THIS CODE. Allow a user to...

PLEASE LET ME KNOW WHERE I AM GOING WRONG IN THIS CODE.

Allow a user to enter any number of double values up to 20. The user should enter 99999 to quit entering numbers.

Display an error message if the user quits without entering any numbers; otherwise, display each entered value and its distance from the average.

My code is below:

import java.util.Scanner;

public class DistanceFromAverage {

public static void main(String[] args) {

// declaration of values as double type

double[] values = new double [20];

int count = 0;

double sum = 0;

double average;

//Declaration of scanner object

Scanner input = new Scanner(System.in);

// Get values from user and iterate the loop up to 20 numbers

for (int i = 0; i < 20; i++) {

//Display statement

System.out.print("Enter the value(99999 to exit):");

double number = input.nextDouble();

// it breaks when values is 99999

if (number == 99999) {

break;

} else {

count++;

values[i] = number;

sum += values[i];

}

}

//checks whether count is zero or not

if (count == 0)

//Display statement for an error message

System.out.printIn("Error! Please enter any values");

else {

//calculation of average

average = sum / count;

// Iterate the loop for calculate distance

for (int i = 0; i < count; i++) {

//Display statement

System.out.printIn(" Distance from " + values[i])

+ " and average :"+ Math.abs(average - values[i])+ " is "

+ average) ;

}

}

}

}

THE ERROR I AM GETTING IS......

DistanceFromAverage.java:39: error: not a statement
+ average) ;
^
DistanceFromAverage.java:39: error: ';' expected
+ average) ;
^
2 errors
Error: Could not find or load main class DistanceFromAverage

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.CODE->

import java.util.Scanner;
public class DistanceFromAverage {

public static void main(String[] args) {
// declaration of values as double type
double[] values = new double [20];
int count = 0;
double sum = 0;
double average;
//Declaration of scanner object
Scanner input = new Scanner(System.in);
// Get values from user and iterate the loop up to 20 numbers
for (int i = 0; i < 20; i++) {
//Display statement
System.out.print("Enter the value(99999 to exit):");
double number = input.nextDouble();
// it breaks when values is 99999
if (number == 99999) {
break;
} else {
count++;
values[i] = number;
sum += values[i];
}
}
//checks whether count is zero or not
if (count == 0)
//Display statement for an error message
System.out.println("Error! Please enter any values");
else {
//calculation of average
average = sum / count;

// Iterate the loop for calculate distance
for (int i = 0; i < count; i++) {
//Display statement
System.out.println(" Distance between " + values[i]
+ " and average "+average+ " is : "
+ Math.abs(average - values[i])) ;
}
}
}
}

2.OUTPUT->

   3.EXPLANATIONS->

(1) In line 31 and 39, you are using capital 'L' in System.out.println() but it is not capital 'i' but small 'L',

[ 'l' for lion ], for better understanding , just change it.

   (2) In line 39, you are using an extra closing parenthesis ')' in System.out.printIn(" Distance from " + values[i] ' ) '

   The indicated ')' is redundant in your code.

  

   Rest of the things are working fine. You can see the corrected version of your code above and also see the    attached output.

After doing above two suggested changes your code will work fine.

Apart from that , the only thing i changed is printing order such that your output seems more consistent..

   It depends on you how you want to print it.

I HOPE YOU WILL GET IT..................YOUR RESPONSE WILL BE HIGHLY APPRECIATED

Add a comment
Know the answer?
Add Answer to:
PLEASE LET ME KNOW WHERE I AM GOING WRONG IN THIS CODE. Allow a user to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Allow a user to enter any number of double values up to 15. The user should...

    Allow a user to enter any number of double values up to 15. The user should enter 99999 to quit entering numbers. Display an error message if the user quits without entering any numbers; otherwise, display each entered value and its distance from the average. DistanceFromAverage.java

  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

  • Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the...

    Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the mean and standard deviation of these numbers I already have the source code all done. I just need this translated into a flowchart. Again I just need the flowchart, not any source code. I have already provided the source code below. Need Flowchart version of my code Here is the source code: public class Mean_Standard_Deviation {    public static void main(String[] args) {   ...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • I need help with the following assignment: Write an application named DailyTemps that continuously prompts a...

    I need help with the following assignment: Write an application named DailyTemps that continuously prompts a user for a series of daily high temperatures until the user enters a sentinel value of 999. Valid temperatures range from -20 through 130 Fahrenheit. When the user enters a valid temperature, add it to a total; when the user enters an invalid temperature, display the error message: Valid temperatures range from -20 to 130. Please reenter temperature. Before the program ends, display the...

  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • write the jave code for the following 1 2 7 B9 4 5 10 11 12...

    write the jave code for the following 1 2 7 B9 4 5 10 11 12 15 13 16 23 24 14 22 17 25 26 18 19 21 20 INRerscrorTro zo Question 11 15 points Save Answer Suppose we have the following code in a Java program: int numbers- (23, 2, 32, 13, 43, 67, 102, 19, 5, 88) System.out.printin ("Please enter a number: ") Scanner console - new Scanner (System.in): int numEntered - console.nextInt (): Add additional lines...

  • I am creating a program where a user will enter in a price amount. And then...

    I am creating a program where a user will enter in a price amount. And then the program will determine how many 20, 10, 5, 1, etc.. they get back. I keep getting "error: unexpected type" in my findValue method. Not too sure why I am getting this. There also might be more bugs in there that I am not aware of because it wont compile. Any help is appreciated. Here is my code: import java.util.Scanner; public class prac1 {...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • I keep getting an Error after I ask the user for test scores. What is missing?...

    I keep getting an Error after I ask the user for test scores. What is missing? package testscores; import java.util.Scanner; public class TestScores { /** * @param args the command line arguments */ private double[] scores;    public TestScores(double[] score) throws IllegalArgumentException { scores = new double[scores.length]; for (int i = 0; i < scores.length; i++) { if (score[i] < 0 || score[i] > 100){ throw new IllegalArgumentException(); } scores[i]=score[i];    }    } public double getAverage() { int sum=0;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT