?
Solved

Cannot figure out what I am doing wrong. Help Needed.

Posted on 2004-10-14
11
Medium Priority
?
321 Views
Last Modified: 2010-03-31
Below is my code
 

import java.io.*;

public class Hw4Pr1{
      public static void main(String[] args) throws IOException {
            BufferedReader stdin  = new BufferedReader (new InputStreamReader(System.in));
            double[] numbers;
            
            for (int i = 0; i < numbers.length; i++) {
            
                  System.out.println("Enter some numbers to calculate the mean and the standart deviation :");
                  String numbersString = stdin.readLine();
            
                  double numbers[i] = Double.parseDouble(numbersString);
                  
            }
            
            System.out.println("The mean is " + mean(numbers));
            System.out.println("The standart deviation is " + deviation(numbers));
      }
}

// Method for computing deviation of double values
public static double deviation(double[] x) {
      double mean = mean(x);
      double squareSum = 0;
      
      for (int i = 0; i < x.length; i++) {
            squareSum += Math.pow(x[i] - mean, 2);
      }
      
      return Math.sqrt((squareSum) / (x.length - 1));
}

// Method for computing deviation of int values
public static double deviation(int[] x) {
      double mean = mean(x);
      double squareSum = 0;
      
      for (int i = 0; i < x.length; i++) {
            squareSum += Math.pow(x[i] - mean, 2);
      }
      
      return Math.sqrt((squareSum) / (x.length - 1));
}

// Method for computing the mean of double values
public static double mean(double[] x) {
      double sum = 0;
      for (int i = 0; i < x.length; i++) {
            sum += x[i];
            }
            
            return sum / x.length;
}

// Method for computing the mean of int values
public static double mean(int[] x) {
      double sum = 0;
      for (int i = 0; i < x.length; i++) {
            sum += x[i];
            }
            
            return sum / x.length;
}

--------------------Configuration: <Default>--------------------
E:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\Hw3Pr1\Hw4Pr1.java:14: ']' expected
                        double numbers[i] = Double.parseDouble(numbersString);
                                       ^
E:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\Hw3Pr1\Hw4Pr1.java:24: 'class' or 'interface' expected
public static double deviation(double[] x) {
              ^
2 errors

Process completed.


Thank you
0
Comment
Question by:Coconut77840
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 12315970
>     double numbers[i] = Double.parseDouble(numbersString);

should be:

    numbers[i] = Double.parseDouble(numbersString);

you also need to initialise your array:

double[] numbers = new double[10];

0
 

Author Comment

by:Coconut77840
ID: 12316001
Thank you

I am still getting one error

public static double deviation(double[] x) {
              ^
1 error

Thanks
0
 
LVL 1

Assisted Solution

by:sammmyg
sammmyg earned 200 total points
ID: 12316004
move the closing  bracket before your deviation to the end of the class definition.

still looking for other error..
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 12316011
move you methods *inside* the class definition
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 100 total points
ID: 12316023
import java.io.*;

public class Hw4Pr1{
     public static void main(String[] args) throws IOException {
          BufferedReader stdin  = new BufferedReader (new InputStreamReader(System.in));
          double[] numbers = new double[10];
          for (int i = 0; i < numbers.length; i++) {

               System.out.println("Enter some numbers to calculate the mean and the standart deviation :");
               String numbersString = stdin.readLine();

               numbers[i] = Double.parseDouble(numbersString);

          }

          System.out.println("The mean is " + mean(numbers));
          System.out.println("The standart deviation is " + deviation(numbers));
     }


// Method for computing deviation of double values
public static double deviation(double[] x) {
     double mean = mean(x);
     double squareSum = 0;

     for (int i = 0; i < x.length; i++) {
          squareSum += Math.pow(x[i] - mean, 2);
     }

     return Math.sqrt((squareSum) / (x.length - 1));
}

// Method for computing deviation of int values
public static double deviation(int[] x) {
     double mean = mean(x);
     double squareSum = 0;

     for (int i = 0; i < x.length; i++) {
          squareSum += Math.pow(x[i] - mean, 2);
     }

     return Math.sqrt((squareSum) / (x.length - 1));
}

// Method for computing the mean of double values
public static double mean(double[] x) {
     double sum = 0;
     for (int i = 0; i < x.length; i++) {
          sum += x[i];
          }

          return sum / x.length;
}

// Method for computing the mean of int values
public static double mean(int[] x) {
     double sum = 0;
     for (int i = 0; i < x.length; i++) {
          sum += x[i];
          }

          return sum / x.length;
}

}
 

compiled


0
 
LVL 1

Expert Comment

by:sammmyg
ID: 12316024
the other error has to do with your instantiation (or lack thereof) of the numbers variable.

instead of making a for loop to count all elts of the array in order to assign them in, (circular reference causing your problem) try to change that for loop to a while loop which will terminate when there is nothing left to read in from the file.

all other code should be fine as is..
0
 

Author Comment

by:Coconut77840
ID: 12316102
It's still not outputting the expected answers.
0
 
LVL 1

Expert Comment

by:sammmyg
ID: 12323003
try changing your main method to this...

   public static void main(String[] args) throws IOException {
          BufferedReader stdin  = new BufferedReader (new InputStreamReader(System.in));
          double[] numbers = new double[1024];
              String dataString = stdin.readLine();
              String [] data = dataString.split(" ");

                        for (int i=0;i<data.length;i++){
                              numbers[i]=Double.parseDouble(data[i]);
                        }


          System.out.println("The mean is " + mean(numbers));
          System.out.println("The standard deviation is " + deviation(numbers));
     }
0
 
LVL 1

Expert Comment

by:sammmyg
ID: 12323252
oops.  hold on.  the problem with the outcome is in calculating x.length which is equal to data.length...

ill post back in a minute...
0
 
LVL 1

Expert Comment

by:sammmyg
ID: 12323408
ok got it.  first of all, you can drop the two tests for when you pass in integers.

since the main method parses input from string to double, you will always be giving doubles.

the problem with calculations is in the fact that arrays are not resizable and we have all been making the array too big, messing up the calculations.

anyway, ive compiled and run this version, and all is well.

check it...


import java.io.*;

public class Hw4Pr1{
     public static void main(String[] args) throws IOException {
          BufferedReader stdin  = new BufferedReader (new InputStreamReader(System.in));
                  double [] numbers = new double[2];
              String dataString = stdin.readLine();
              String [] data = dataString.split(" ");

                        int j = determineSize(data);

                        for (int i=0;i<j;i++){
                              numbers[i]=Double.parseDouble(data[i]);
                        }


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


public static int determineSize(String [] rawData){
      int i=0;
      int numberNonNull =0;
      boolean isNotNull;
      do{
            if(rawData[i]!=null){
                  isNotNull = true;
                  numberNonNull += 1;
            }else{isNotNull = false;}
            i+=1;


      }while(isNotNull==true && i<rawData.length);

      return numberNonNull;
}


// Method for computing deviation of double values
public static double deviation(double[] x) {
     double mean = mean(x);
     double squareSum = 0;

     for (int i = 0; i < x.length; i++) {
          squareSum += Math.pow(x[i] - mean, 2);
     }

     return Math.sqrt((squareSum) / (x.length - 1));
}



// Method for computing the mean of double values
public static double mean(double[] x) {
     double sum = 0;
     for (int i = 0; i < x.length; i++) {
          sum += x[i];
          }

          return sum/x.length;
}


}
0
 
LVL 1

Expert Comment

by:sammmyg
ID: 12323550
CRAP.

sorry peoples.  i think i posted b4 a little prematurely. forgot to clean up my mess there. :D
 ignore above postings and compile this.  


import java.io.*;

public class Hw4Pr1{
     public static void main(String[] args) throws IOException {
          BufferedReader stdin  = new BufferedReader (new InputStreamReader(System.in));
                  double [] numbers = new double[1024];
              String dataString = stdin.readLine();
              String [] data = dataString.split(" ");

                        int j = determineSize(data);

                        for (int i=0;i<j;i++){
                              numbers[i]=Double.parseDouble(data[i]);
                        }


          System.out.println("The mean of is " + mean(numbers, j));
          System.out.println("The standard deviation is " + deviation(numbers, j));
     }


public static int determineSize(String [] rawData){
      int i=0;
      int numberNonNull =0;
      boolean isNotNull;
      do{
            if(rawData[i]!=null){
                  isNotNull = true;
                  numberNonNull += 1;
            }else{isNotNull = false;}
            i+=1;


      }while(isNotNull==true && i<rawData.length);

      return numberNonNull;
}


// Method for computing deviation of double values
public static double deviation(double[] x, int j) {
     double mean = mean(x, j);
     double squareSum = 0;

     for (int i = 0; i < x.length; i++) {
          squareSum += Math.pow(x[i] - mean, 2);
     }

     return Math.sqrt((squareSum) / j);
}



// Method for computing the mean of double values
public static double mean(double[] x, int j) {
     double sum = 0;
     for (int i = 0; i < x.length; i++) {
          sum += x[i];
          }

          return sum/j;
}


}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question