the question im doing is:
write a java program that allows the user to enter a set of numbers and then displays the sum and the average of the values. It should begin by asking the user how many values they will enter. you should use two seperate methods to compute the sum and the average.(note that the average is the sum divided by the number of values, so the average method should call the sum method)
This is what I have so far:

import javax.swing.JOptionPane;
public class maths_comp
{
	public static void main(String[]args)
	{
		//Declare variables
		String input;
		double iterations;
		double Sum;
		
		input = JOptionPane.showInputDialog("Enter the amount of numbers to be entered:");
		iterations = Double.parseDouble(input);
		
		JOptionPane.showMessageDialog(null, "The Sum of the " + iterations +  " numbers entered is: " + sum(iterations) + "\nThe Average of the numbers entered is: " + average(iterations));
		
		
		
	}
	
	public static double sum(double iter)
	{
		String SumInput;
		double NewNum;
		int count = 1;
		double Total = 0;
		
		while(count <= iter)
		{
			SumInput = JOptionPane.showInputDialog("Enter number " + count + ":");
			NewNum = Double.parseDouble(SumInput);
			Total = Total + NewNum;
			count = count + 1;
		}
		return Total;
	}
	
	public static double average(double iter)
	{
		double AvgNum;
		AvgNum = sum(iter) / iter;
		return AvgNum;
	}

}

The problem im having is that my program asks for the values again when calculating the average...how do I get it to ask the user only once?

how about this:

public static double sum(double iter)
{
String SumInput;
double NewNum;
int count = 1;
double Total = 0;
 
while(count <= iter)
{
SumInput = JOptionPane.showInputDialog("Enter number " + count + ":");
NewNum = Double.parseDouble(SumInput);
Total = Total + NewNum;
count = count + 1;
}
System.out.println("The sum = " + Total);
return Total;
}
 

public static double average(double iter)
{
double AvgNum;
AvgNum = sum(iter) / iter;
return AvgNum;
}

public static void main(String[]args)
	{
		//Declare variables
		String input;
		double iterations;
		double Sum;
		
		input = JOptionPane.showInputDialog("Enter the amount of numbers to be entered:");
		iterations = Double.parseDouble(input);
        double av = average(iterations);
        System.out.println("the average is: " + av);
		
	}

another approach would be: read the numbers into an array before calling the methods, and pass this array as a parameter.
you wouldn't call your input twice then either.

how can this question be solved using arrays so that the sum and average method are only calculating the sum and average.

to initialize an array, without knowing the contents before, you need two things: the type of the element you want to store, and the number of elements

for instance: you want an array of ten integers (the primitives)

int[] arrayWithTenNumbers = new int[10];

this array has room for ten int elements, which you can store or ask using the index it has in the array. the index of an array always starts with 0, so the highest index possible is: number of elements - 1 (9, in this case)

if you want to enter the ten numbers:

for (int i = 0; i < 10; i++ ){
  arrayWithTenNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number:"));
}

this will help you get started. now, try and see if you can put this logic in your code, see if you can make it dynamically (so that instead of 10, you refer to the length of the array) ..

Another suggestion in order to ask for the numbers once is to ask the user to separate each number by a separator you prefer maybe a comma or a whitespace.

Split the string using the separator.

String input = JOptionPane.showInputDialog("Enter the numbers separated by a comma:");
//split the string using comma.
String numbersInStrings[] = input.split(",");
Double sum = 0.0;
//get the sum
for(String number:numbersInStrings){
    sum = sum + Double.parseDouble(number);
}
//output the average note average = sum/length of the array
JOptionPane.showMessageDialog(null,"Average:"+average(sum,numbersInStrings.length);

You can improve this code to check if the last character is a comma, whitespace between each. You can trim the whitespace.

considering the fact the OP does not yet know how to use arrays, I would not recommend him to just copy paste this without first trying to understand the whole of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.