0
package deviation;
import java.util.Scanner; 

public class Deviation { 
public static void main(String[] args) { 
Scanner input = new Scanner(System.in); 

System.out.println("Enter n: "); 
int n = input.nextInt(); 
System.out.println("Enter " + n + " numbers: "); 
double[] x = new double[n]; 
int i; 
for (i = 0; i<n; i++) {
        x[i] = input.nextDouble();
    } 

double mean = mean(x, i); 
double deviation = deviation(x, i); 

System.out.println("The mean is " + mean); 
System.out.println("The standard deviation is " + deviation); 

} 
public static double deviation(double[] x, double i) { 
double mean = mean(x, i); 
double num = 0; 
num += Math.pow(x[i] - mean, 2); 
double n;

 double den = n - 1;
double deviation = Math.sqrt((num/den)); 
return deviation; 
} 
public static double mean(double[] x, double i) { 
double sum = 0; 

for(int j=0;j<i;j++) { 
sum += x[j]; 
} 
return sum / i; 
}

} 
2
Contributors
7
Replies
21
Views
2 Years
Discussion Span
Last Post by sejalp26
0

I am geting error(red caution mark) at line 27 and even after running it it just prompts to enter number and then says :

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - possible loss of precision
  required: int
  found:    double
    at deviation.Deviation.deviation(Deviation.java:27)
    at deviation.Deviation.main(Deviation.java:18)
Java Result: 1
0

No time now to read the code, but...
the message is clear - you have used double where an int is required. The compiler won't allow an automatic conversion because doubles can have fractional values but ints can't.

0

yes thats true. but can't change it to int because values can be double also and it is actually double but still i tried to change it to int then also nothing happening.

0

I presume it's referring to your mean method - the second parameter isn't a data value, it's some kind of aray index or size, which therefore has to be an integer type.

0

will you plz look at code abovce and let me know what is supposed to be done exactly because I have tried evrything but nothing working. It would be a great help . Thank u

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.