#Array Syntax exception

141 messages · Page 1 of 1 (latest)

ripe gazelle
#

There is an error for Standard Deviation, and the values are error on Sum and Mean too

rocky mango
#

helllo

#

any code there?

ripe gazelle
#
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

rocky mango
#

which is the 75th line

ripe gazelle
rocky mango
#
        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

ripe gazelle
#

The values are not correct

#

1.458 and 1.466 🤔

#

not completely solved

rocky mango
#

i think so its probelm of precistion

ripe gazelle
#

?

rocky mango
#

presizion

ripe gazelle
#

how to solve this issue

rocky mango
#

or maybe code bug

rocky mango
ripe gazelle
#

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?

rocky mango
#

can u send full code

#

new one

ripe gazelle
#

idk why "0.1+0.2≠0.3" happens

rocky mango
#

wow u didnt changed anything i said

ripe gazelle
#

?

rocky mango
ripe gazelle
rocky mango
#

i said u

#

u are doing i = 1

#

array has 0th index too

ripe gazelle
rocky mango
#

his = {hello,hi,hiii}
his[0] will be hello

#

is it working now??

ripe gazelle
#

no

rocky mango
#

sum also not working??

ripe gazelle
#

btw correct answer of Standard Deviation would be this (by Calculator)

ripe gazelle
#

both Sum and Mean

rocky mango
#

can u send new code again

ripe gazelle
rocky mango
#

how can sum be wrong

#

are u sure its wrong??

ripe gazelle
#

why the sum isn't "7.41" but "7.409999999999999"

rocky mango
ripe gazelle
#

?

rocky mango
#

does that matter??

ripe gazelle
#

yes

rocky mango
#

so it should be formatted to 0.00

ripe gazelle
#

still ducked-up

rocky mango
#

DecimalFormat form = new DecimalFormat("0.00");
System.out.println(form.format(0.1292));
this will print 0.13

#

take hthis

ripe gazelle
#

and there are various numbers

rocky mango
#

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

ripe gazelle
rocky mango
#

add import

#

import java.text.DecimalFormat;

ripe gazelle
#

?

rocky mango
#

add the import

#

at starting

ripe gazelle
#

Wrong value at "Standard Deviation"

ripe gazelle
#

do you know how to calculate it correctly

rocky mango
#

no

#

idk

#

one sec

ripe gazelle
rocky mango
ripe gazelle
#

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));
        }
rocky mango
ripe gazelle
#

?

#

oh

#

I forgot (-1)

#

but still not 1.00

#

idk how to calculate it

rocky mango
#

nvm

#

u input wrong number??

ripe gazelle
#

I didn't

ripe gazelle
#

hi?

ripe gazelle
#

@rocky mango hi?

rocky mango
#

hi

ripe gazelle
#

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));
        }
rocky mango
#

ur calculation wrong

#

try to debug

ripe gazelle
#

idk where is it wrong

rocky mango
#

what is the expected and what was actual output???

ripe gazelle
#

erm

#

wrong value

#

why

#

🤔

rocky mango
#
      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

ripe gazelle
#

nope

#

still wrong

#

not "1.64"

rocky mango
#

itsnot possible its not correct

rocky mango
#

show me ur corrent code

ripe gazelle
#

it shows 1.27

#

not 1.64

#

Don't try to change the length of array as "5" since it must be "10" in total