5 Replies - 451 Views - Last Post: 16 July 2012 - 07:29 PM Rate Topic: -----

#1 PMG1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-July 12

Averaging Pgm using an Array

Posted 16 July 2012 - 04:40 PM

I am attempting to create a program using an array that will allow you to enter any amount of numbers and give you their average. I have it nearly complete, but I am getting a NullPointerException if I do not enter as many values as I specified in the first question. I want to be able to type in 666 at any point and still calculate the average, regardless of how many numbers I claimed to be averaging. I don't know if this is possible, I'm just doing this program for learning experience, please help.

Work flow for Error:
1. Answer first question with: 5
2. Enter 2 numbers, then enter 666

The error comes on line 37 around the second for loop, it wont allow me to add just the values already entered. The only time it works is when you fill the array with the amount of numbers you previously specified. I hope this is easily understandable, thanks.

[code]

package average;

import java.util.Scanner;

public class Average {

/**
* @param args
*/
public static void main(String[] args) {
Scanner ui = new Scanner(System.in);
Double[] array;
double total = 0;

System.out.print("How many numbers would you like to average? ");
int num = ui.nextInt();

array = new Double[num];

Scanner input = new Scanner(System.in);
// Fills array with numbers given by user to be averaged
for(int i = 0; i < array.length; i++){

System.out.print("Please enter a number to be averaged and press enter: ");
double num2 = input.nextDouble();
if(num2 == 666){
break;
}else{
array[i] = num2;

}
}

// adds all the numbers given by the user and puts them into the total variable
for(int z = 0; z < array.length; z++){

total += array[z];


}

// finds the average of all the numbers given by the user
double average = total / array.length;


System.out.println("The average of your numbers is: " + average);

}

}


[\code]

Is This A Good Question/Topic? 0
  • +

Replies To: Averaging Pgm using an Array

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Averaging Pgm using an Array

Posted 16 July 2012 - 05:05 PM

I'm just amazed how you managed to make a \ instead of a normal slash /
Was This Post Helpful? 0
  • +
  • -

#3 PMG1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-July 12

Re: Averaging Pgm using an Array

Posted 16 July 2012 - 05:12 PM

View PostCasiOo, on 16 July 2012 - 05:05 PM, said:

I'm just amazed how you managed to make a \ instead of a normal slash /


I was quite amazed that there is not edit button...
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Averaging Pgm using an Array

Posted 16 July 2012 - 05:42 PM

View PostPMG1989, on 17 July 2012 - 12:12 AM, said:

View PostCasiOo, on 16 July 2012 - 05:05 PM, said:

I'm just amazed how you managed to make a \ instead of a normal slash /


I was quite amazed that there is not edit button...


There is but maybe disabled for new users.

You make an array with room for 666 Double's but you dont put in a Double for every place. When you lookup an index in the array that has not been set to anything, you will get null.

If you used the primitive type double for your array, it would initialize the array with the value 0.0.
Was This Post Helpful? 0
  • +
  • -

#5 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Averaging Pgm using an Array

Posted 16 July 2012 - 05:48 PM

View PostPMG1989, on 16 July 2012 - 07:40 PM, said:

[ code]

[\code]


Hey, at least he tried :lol:

--

You need to initialize your array. If you are telling the machine that you want to enter 50 numbers and you only initialize the first 10, the other 40 will be null and you will have an NullPointerException:

Add this code after you declare the size of your array.

for (int i = 0; i < num; i++) {
  array[i] = 0.0;
}


This will set every value in the array to zero.

You may need to keep a counter and increment only when a valid value is being inserted into the array. ...or you could just use 'num', you decide...

This post has been edited by x68zeppelin80x: 16 July 2012 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

#6 PMG1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-July 12

Re: Averaging Pgm using an Array

Posted 16 July 2012 - 07:29 PM

That works, thank you both.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1