#Array Syntax exception
141 messages · Page 1 of 1 (latest)
import java.util.Scanner;
public class StandardDeviation {
public static void main(String [] args) {
//Declare Numbers
double ansOutput;
double valueNum[] = new double[10];
double numInput = 0;
Scanner keyInput = new Scanner(System.in);
int numN = 0;
//Question and Declare Values
System.out.println("Enter negative value to end the input section!");
for (int ansCount=1; ansCount<=10; ansCount++){
System.out.print("Please enter the value: ");
ansOutput = keyInput.nextDouble();
if (ansOutput < 0)
break;
valueNum[ansCount] = ansOutput;
numN++;
}
//Calculation (Sum of value)
double sumResult = 0;
for (int sumCount = 0; sumCount < valueNum.length; sumCount++) {
sumResult += valueNum[sumCount];
}
System.out.println("Sum = "+sumResult);
//Calculation (Mean value)
double meanResult = 0;
meanResult = sumResult / numN;
System.out.println("Mean = "+meanResult);
//(Maximum and Minimum section, omitted)
//Calculation (Standard Deviation)
double standardDeviationResult = 0;
double devResult = 0;
double devPlusResult = 0;
if (numN <= 1)
System.out.println("Standard Deviation = NaN");
else {
for (int devCount = 0; devCount < numN; devCount++) {
valueNum[devCount] = Math.pow(valueNum[devCount]-meanResult, 2);
}
for (int devPlusCount = 0; devPlusCount < numN; devPlusCount++) {
devPlusResult = valueNum[devPlusCount] + valueNum[devPlusCount-1];
}
devResult = devPlusResult/(numN-1);
standardDeviationResult = Math.sqrt(devResult);
System.out.println("Standard Deviation = "+standardDeviationResult);
}
}
}
Original Question
wait a sec
which is the 75th line
for (int ansCount=1; ansCount<=10; ansCount++){
System.out.print("Please enter the value: ");
ansOutput = keyInput.nextDouble();
if (ansOutput < 0)
break;
valueNum[ansCount] = ansOutput;
numN++;
}
problem with this is anscount is 1 and the starting of array is 0 not 1
for (int i=0; i<10; i++){
System.out.print("Please enter the value: ");
ansOutput = keyInput.nextDouble();
if (ansOutput < 0)
break;
valueNum[i] = ansOutput;
}
this is corent
correct
it will do for 10 times and it will asign to correct array index
any array first index is 0
so last will be its size minus 1
for (int devPlusCount = 0; devPlusCount < numN; devPlusCount++) {
devPlusResult = valueNum[devPlusCount] + valueNum[devPlusCount-1];
}
error is in here
because ua re doing 0-1 for first time in loop so arrayoutofindex exceoption throwing
for (int devPlusCount = 0; devPlusCount < numN-1; devPlusCount++) {
devPlusResult = valueNum[devPlusCount+1] + valueNum[devPlusCount];
}
``` this will fix it maybe because it will take devPlusCount < numN-1 as the stop point in for loop and its doing plus the valuenum[devcount+1]
for (int devPlusCount = 1; devPlusCount < numN; devPlusCount++) {
devPlusResult = valueNum[devPlusCount] + valueNum[devPlusCount-1];
}
this will work too its taking the starting devpluscount as 1
now u can close this if its solved
i think so its probelm of precistion
?
presizion
how to solve this issue
or maybe code bug
are the sum and other thigns coming correct other then deviation
Sum and Mean are errored
//Calculation (Sum of value)
double sumResult = 0;
for (int sumCount = 0; sumCount < valueNum.length; sumCount++) {
sumResult += valueNum[sumCount];
}
System.out.println("Sum = "+sumResult);
//Calculation (Mean value)
double meanResult = 0;
meanResult = sumResult / numN;
System.out.println("Mean = "+meanResult);
Original Code
hi?
wow u didnt changed anything i said
?
t
just check up
change to ths
tbh why this affects
no
sum also not working??
btw correct answer of Standard Deviation would be this (by Calculator)
can u send new code again
bruh sum is working??
?
does that matter??
yes
so it should be formatted to 0.00
still ducked-up
DecimalFormat form = new DecimalFormat("0.00");
System.out.println(form.format(0.1292));
this will print 0.13
take hthis
where should it put
and there are various numbers
declare decimal fomrat above and when print numbers use form.format(num)
take
i added to every system.println
if u dont want soem to be formatted remove form.form from it
?
.
do you know how to calculate it correctly
erm
so...
it is a big error
//Calculation (Standard Deviation)
double standardDeviationResult = 0.00;
double devPlusResult = 0.00;
if (numN <= 1)
System.out.println("Standard Deviation = NaN");
else {
for (int devCount = 0; devCount < numN; devCount++) {
valueNum[devCount] = Math.pow(valueNum[devCount]-meanResult, 2);
}
for (int devPlusCount = 1; devPlusCount < numN; devPlusCount++) {
devPlusResult = valueNum[devPlusCount] + valueNum[devPlusCount-1];
}
standardDeviationResult = Math.sqrt(devPlusResult/(numN-1));
System.out.println("Standard Deviation = "+form.format(standardDeviationResult));
}
hi?
@rocky mango hi?
hi
so
why it was wrong
//Calculation (Standard Deviation)
double standardDeviationResult = 0.00;
double devPlusResult = 0.00;
if (numN <= 1)
System.out.println("Standard Deviation = NaN");
else {
for (int devCount = 0; devCount < numN; devCount++) {
valueNum[devCount] = Math.pow(valueNum[devCount]-meanResult, 2);
}
for (int devPlusCount = 1; devPlusCount < numN; devPlusCount++) {
devPlusResult = valueNum[devPlusCount] + valueNum[devPlusCount-1];
}
standardDeviationResult = Math.sqrt(devPlusResult/(numN-1));
System.out.println("Standard Deviation = "+form.format(standardDeviationResult));
}
idk where is it wrong
what is the expected and what was actual output???
double sum = 0.0, standard_deviation = 0.0;
int array_length = valueNum.length;
for(double temp : valueNum) {
sum += temp;
}
double mean = sum/array_length;
for(double temp: valueNum) {
standard_deviation += Math.pow(temp - mean, 2);
}
double result = Math.sqrt(standard_deviation/array_length);
System.out.println("Standard Deviation = "+form.format(result));
try this
this is correct
itsnot possible its not correct