Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need help making program

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help making program

    Hey guys I have to create a program that computes the mean, standard deviation, and finds the max and min of a group of numbers read in from a file. I'm having trouble storing the objects from the file in an array and then reading it to compute the mean. Here's what I have so far:

    import java.io.*;
    import java.util.*;
     
    public class Assignment5
    {
     
     
        public static void main(String[] args) throws FileNotFoundException
        {
            Scanner input = new Scanner(new File("nerd.txt"));
            double[] data = new double[1000];
            double mean = 0;
            double sum = 0;
            double n = 0;
     
            while (input.hasNextDouble()){
    			double element = input.nextDouble();
    			data[element]++;
    			n++;
     
    		}
     
     
    		for(int x = 0; x < data.length; x++){
    		    sum = sum + data[x] * x;
    		}
    		mean = (double)sum / n;
            System.out.println("The mean is " + mean);
    	}
     
     
     
    }

    I don't really understand arrays. But basically I have to: Use an array to store the numbers being read. Keep a count each time a number is read (this will be N).

    I just really don't know how to store the numbers from the file :/. Any help will be greatly appreciated


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Need help making program

    Hello there.

    I believe you need to change your while loop slightly.

    while (input.hasNextDouble()) {
        double element = input.nextDouble();
        data[n] = element;
        n++;
    }

    Doing it this way means that each element you reach from the file gets stored in the array in position n which is incremented after each store. I know this is just a basic program to get this going however you might want to think about what happens when there are 1001 or more numbers in file, your array will not be able to cope.

    Happy programming and good luck

    // Json

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help making program

    Hmm, so how should i declare the array in the main?

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Need help making program

    Its declared fine as it is right now.

    double[] data = new double[1000];

    Or did you mean if you want to avoid running into an ArrayIndexOutOfBoundsException because of the size?

    Well you could use an ArrayList for instance as it will grow automagically as you populate it with more data.

    // Json

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help making program

    Alrighty, hopefully now my logic will be correct and I'll be finished. Woo! Just one thing...when I go to compile, it says i declared "n" as a double, but an int is required. I didn't declare anythign as an int in my progrm so I don't undersand what is going on here...
    Last edited by ixjaybeexi; December 6th, 2009 at 05:03 PM.

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help making program

    Ok, I got a bit of it done, it's just that when there's a negative number in the list, it prints out the mean is NaN. An suggestions on how to fix my code to constitute for negative numbers?

Similar Threads

  1. [SOLVED] How to start writing java mobile application?
    By Kok in forum Java ME (Mobile Edition)
    Replies: 15
    Last Post: July 30th, 2009, 01:52 AM
  2. Digital map application with Java GUI
    By donjuan in forum AWT / Java Swing
    Replies: 3
    Last Post: May 15th, 2009, 03:32 AM
  3. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM