Hey, I'm working on a problem and what I need the program to do is to add up the numbers in the array NOT add up how many numbers there are. There are 5 total numbers that a user will input. The counting of the numbers will be in a seperate method which will then return to the main. Then be displayed through system.out.println. I don't know where I am going wrong any help? Thanks!

import javax.swing.*;
import java.util.*;
public class Question10 {

public static void main (String []args) {

//declares
Scanner keyboard = new Scanner(System.in);

int [] myNums = new int[5];
for (int i=0; i < myNums.length; i++) {
System.out.println ("Please Enter Data");
 myNums[i]= keyboard.nextInt();
}




} //end main


public static int getTotal(int inArray) {

int a,b,c,d,e,f;
f = a+b+c+d+e;

return f;
}

}//end class

But where are you printing the result? You are now using the getTotal() function.

I would write something like this:

class main
{
        public static void main(String args[])
        {
                int arr[] = {1,2,3,4,5};

                System.out.println("Sum of the array arr is: " + arraySum(arr)); 
        }

        public static int arraySum(int arr[])
        {
                int result = 0;

                for(int i = 0; i < arr.length; i++)
                        result += arr[i]; // go throw each elelement in the array and add the into result
             
                return result;
        }// returns the sum of the array
}

hope it helps :)

import java.util.Scanner;

public class Question10{

	public static void main(String[] args) {
		int [] myNums = new int[5];
		System.out.println("The total is " + Method(myNums)); 	
		
	}
	
	public static int method(int myNums[]){
		Scanner keyboard = new Scanner(System.in);
		int total=0;
		for (int i=0; i < myNums.length; i++) {
			System.out.println ("Please Enter Data ");
			myNums[i]= keyboard.nextInt();
			total +=myNums[i]; // add each to total
		}
		return total;
	}

}

That should work, but it says: "The counting of the numbers will be in a seperate method which will then return to the main."

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.