12 Replies - 568 Views - Last Post: 01 April 2011 - 01:25 PM Rate Topic: -----

#1 CaptainCornbread  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-April 11

Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 12:07 PM

Good afternoon!

I am a newbie to Java and am trying to work through an issue. I have written a little program that collects between 2 and 25 values from the user, stores them in an array, and then provides the average.

When I compile my code, I get an error that states "Variable numberOfValues might not have been initialized", and I can't run it. I feel like I am very close but just can't get over the hump. Thank you in advance for you assistance.

>>>Rich

import java.util.Scanner;

public class Assignment11
{
	public static void main(String[] args)
	{
	Scanner keyboard = new Scanner(System.in);

	int numberOfValues;
	double runningTotal = 0, valuesAverage;
	double[] values = new double[numberOfValues];
	// input the number of values
	System.out.print("What is the number of values? (Must be between 2 and 25): ");

	if ((numberOfValues < 2) || (numberOfValues > 25))
	{
		System.out.print("The number of values must be between 2 and 25!");	
	}

	for (int x = 0; x < numberOfValues; x++)
	{
		// input values
		values[x] = keyboard.nextDouble();
		runningTotal = values[numberOfValues];
	}

	// calculate the average
	runningTotal = runningTotal + values[numberOfValues];
	valuesAverage = runningTotal / numberOfValues;
	
	// output the average
	System.out.println("The average of the values is: " + valuesAverage);
	}
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Variable initialization error - calculating ave of values in an array

#2 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 12:19 PM

hi you can let the user enter the numberOfValues for you because you cant create an array with undeclared or unknown number of elements.
Was This Post Helpful? 2
  • +
  • -

#3 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 202
  • Joined: 11-December 10

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 12:20 PM

Change line 9 to int numberOfValues = 0; and then add new line
numberOfValues = keyboard.nextInt();

Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 12:20 PM

Your problem is that you have used that variable before initializing a value to it
// you declare here
int numberOfValues;
	double runningTotal = 0, valuesAverage;
	double[] values = new double[numberOfValues];
	// input the number of values
	System.out.print("What is the number of values? (Must be between 2 and 25): ");
//put here
numberOfValues = keyboard.nextInt();

	if ((numberOfValues < 2) || (numberOfValues > 25))// you use it here before giving it a value
	{
		System.out.print("The number of values must be between 2 and 25!");	
	}

Was This Post Helpful? 1
  • +
  • -

#5 CaptainCornbread  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-April 11

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 12:31 PM

OK, I think we're closer. I incorporated smohd's advice. It now compiles and executes. I tested as follows:

What is the number of values? (Must be between 2 and 25): 4
80 82.1 99 100

Now I am getting a different error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Assignment11.main(Assignment11.java:29)

Thanks again for your help, everyone. It is greatly appreciated.


import java.util.Scanner;

public class Assignment11
{
	public static void main(String[] args)
	{
	Scanner keyboard = new Scanner(System.in);

	int numberOfValues = 0;

	double runningTotal = 0, valuesAverage;
	double[] values = new double[numberOfValues];
	// input the number of values
	System.out.print("What is the number of values? (Must be between 2 and 25): ");
	numberOfValues = keyboard.nextInt();
	if ((numberOfValues < 2) || (numberOfValues > 25))
	{
		System.out.print("The number of values must be between 2 and 25!");	
	}

		for (int x = 0; x < numberOfValues; x++)
		{
			// input values
			values[x] = keyboard.nextDouble();
			runningTotal = values[numberOfValues];
		}

	// calculate the average
	runningTotal = runningTotal + values[numberOfValues];
	valuesAverage = runningTotal / numberOfValues;
	
	// output the average
	System.out.println("The average of the values is: " + valuesAverage);
	}
} 




Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 12:43 PM

Your problem again is that you make your array with size zero, look:
//first you make this to zero
int numberOfValues = 0;

	double runningTotal = 0, valuesAverage;
//then you use it as a size ==0
	double[] values = new double[numberOfValues];//you see!!
	// input the number of values
	System.out.print("What is the number of values? (Must be between 2 and 25): ");
// then you change the numberOfValues to another value between 2 and 25
	numberOfValues = keyboard.nextInt();

Was This Post Helpful? 1
  • +
  • -

#7 CaptainCornbread  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-April 11

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 12:58 PM

Thank you smohd. I understand what you mean. I removed the numberOfValues initialization so it is no longer set to 0, and I also moved the array declaration down under where I have had the user enter a value for numberOfValues. Now the program runs, and everything works - except that I am getting an average of 0.0 no matter what values I enter. I tested as follows:

What is the number of values? (Must be between 2 and 25): 3
99 92 100
The average of the values is: 0.0

Now I know I am close! Please advise. And thank you again for your advice today - it has been very helpful.

import java.util.Scanner;

public class Assignment11
{
	public static void main(String[] args)
	{
	Scanner keyboard = new Scanner(System.in);

	int numberOfValues;
	double runningTotal = 0, valuesAverage;
	
	// input the number of values
	System.out.print("What is the number of values? (Must be between 2 and 25): ");
	numberOfValues = keyboard.nextInt();
	if ((numberOfValues < 2) || (numberOfValues > 25))
	{
		System.out.print("The number of values must be between 2 and 25!");	
	}
	double[] values = new double[numberOfValues + 1];
		for (int x = 0; x < numberOfValues; x++)
		{
			// input values
			values[x] = keyboard.nextDouble();
			runningTotal = values[numberOfValues];
		}

	// calculate the average
	runningTotal = runningTotal + values[numberOfValues];
	valuesAverage = runningTotal / numberOfValues;
	
	// output the average
	System.out.println("The average of the values is: " + valuesAverage);
	}
} 


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10560
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 01:03 PM

No need for an array at all here. Just keep adding elements to runningTotal in the loop, then divide runningTotal/numItems after the loop. Note that in the loop, you are currently overwriting the value in runningTotal here runningTotal = values[numberOfValues];, rather than adding to it.
Was This Post Helpful? 0
  • +
  • -

#9 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 01:03 PM

View PostCaptainCornbread, on 01 April 2011 - 12:58 PM, said:

Thank you smohd. I understand what you mean. I removed the numberOfValues initialization so it is no longer set to 0, and I also moved the array declaration down under where I have had the user enter a value for numberOfValues. Now the program runs, and everything works - except that I am getting an average of 0.0 no matter what values I enter. I tested as follows:

What is the number of values? (Must be between 2 and 25): 3
99 92 100
The average of the values is: 0.0

Now I know I am close! Please advise. And thank you again for your advice today - it has been very helpful.

import java.util.Scanner;

public class Assignment11
{
	public static void main(String[] args)
	{
	Scanner keyboard = new Scanner(System.in);

	int numberOfValues;
	double runningTotal = 0, valuesAverage;
	
	// input the number of values
	System.out.print("What is the number of values? (Must be between 2 and 25): ");
	numberOfValues = keyboard.nextInt();
	if ((numberOfValues < 2) || (numberOfValues > 25))
	{
		System.out.print("The number of values must be between 2 and 25!");	
	}
	double[] values = new double[numberOfValues + 1];
		for (int x = 0; x < numberOfValues; x++)
		{
			// input values
			values[x] = keyboard.nextDouble();
			runningTotal = values[numberOfValues];
		}

	// calculate the average
	runningTotal = runningTotal + values[numberOfValues];
	valuesAverage = runningTotal / numberOfValues;
	
	// output the average
	System.out.println("The average of the values is: " + valuesAverage);
	}
} 


remove
runningTotal = values[numberOfValues];


and cut
runningTotal = runningTotal + values[x];


in to the for loop

This post has been edited by kngofdrkns: 01 April 2011 - 01:05 PM

Was This Post Helpful? 2
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 01:06 PM

Ok change this:
double[] values = new double[numberOfValues + 1];
		for (int x = 0; x < numberOfValues; x++)
		{
			// input values
			values[x] = keyboard.nextDouble();
			runningTotal = values[numberOfValues];
		}

	// calculate the average
	runningTotal = runningTotal + values[numberOfValues];


to become:
double[] values = new double[numberOfValues + 1];
		for (int x = 0; x < numberOfValues; x++)
		{
			// input values
			values[x] = keyboard.nextDouble();
		runningTotal = runningTotal + values[x];	
		}

valuesAverage = runningTotal / numberOfValues;

Was This Post Helpful? 1
  • +
  • -

#11 CaptainCornbread  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-April 11

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 01:11 PM

SUCCESS!

smohd, thanks again for everything this afternoon. And you too kngofdrkns. I bow before your massiveness.
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 01:13 PM

You are welcomed again and again
Was This Post Helpful? 0
  • +
  • -

#13 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: Variable initialization error - calculating ave of values in an array

Posted 01 April 2011 - 01:25 PM

you are welcomed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1