# Averaging Pgm using an Array

Page 1 of 1

## 5 Replies - 696 Views - Last Post: 16 July 2012 - 07:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=285997&amp;s=b82dd2b3902e391b1d0871018a028f24&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 PMG1989

• New D.I.C Head

Reputation: 0
• 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

• D.I.C Lover

Reputation: 1524
• Posts: 3,401
• 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

• New D.I.C Head

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

## Re: Averaging Pgm using an Array

Posted 16 July 2012 - 05:12 PM

CasiOo, 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

• D.I.C Lover

Reputation: 1524
• Posts: 3,401
• Joined: 05-April 11

## Re: Averaging Pgm using an Array

Posted 16 July 2012 - 05:42 PM

PMG1989, on 17 July 2012 - 12:12 AM, said:

CasiOo, 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

• D.I.C Addict

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

## Re: Averaging Pgm using an Array

Posted 16 July 2012 - 05:48 PM

PMG1989, on 16 July 2012 - 07:40 PM, said:

[ code]

[\code]

Hey, at least he tried

--

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

• New D.I.C Head

Reputation: 0
• 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

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}