Search code examples
javarecursionsum

Getting a Sum to print out from a user defined array summed recursively


I'm trying to get my program to print out the sum of an array of elements that are user defined. I have most of it done, but I get an error at the end where I want to print out the sum. Here's the program I have and the error is from the sumArray.length in the main class' print line:

import java.util.*;
/**
 * SumArray
 *
 * @author (Sean Hall)
 * @version (4/13/21)
 */
public class SumArray
{   
    public static void main(String[] args)
    {
        int sumArray = makeArray();
        System.out.println("Sum of numbers equals: " + findSum(sumArray, sumArray.length)); //.length here gives the error
    }
     
    public static int makeArray()
    {
        System.out.print("Enter the total number of integers you want to sum up: ");
        Scanner keyboard = new Scanner(System.in);
        int size = keyboard.nextInt();
        int sumArray[] = new int [size];
        System.out.println("Enter the numbers to add, one value at a time: ");
        for(int i = 0; i < size; i++)
        {
            sumArray[i] = keyboard.nextInt();
        }
    }
    
    public static int findSum(int sumArray[], int i)
    {
        if(i > 1)
        {
            return sumArray[i];
        }
        else
        {
            return sumArray[i] + findSum(sumArray, i-1);
        }
    }
}

I think I got it mostly correct, but do correct me if I did anything else wrong, I'm taking a Java intro class and this is one of my assignment problems.


Solution

  • import java.util.*;
    /**
     * SumArray
     *
     * @author (Sean Hall)
     * @version (4/13/21)
     */
    public class SumArray
    {
        public static void main(String[] args)
        {
            int[] sumArray = makeArray();
            System.out.println("Sum of numbers equals: " + findSum(sumArray, sumArray.length-1)); //.length here gives the error
        }
    
        public static int[] makeArray()
        {
            System.out.print("Enter the total number of integers you want to sum up: ");
            Scanner keyboard = new Scanner(System.in);
            int size = keyboard.nextInt();
            int[] sumArray = new int[size];
            System.out.println("Enter the numbers to add, one value at a time: ");
            for(int i = 0; i < size; i++)
            {
                sumArray[i] = keyboard.nextInt();
            }
            return sumArray;
        }
    
        public static int findSum(int sumArray[], int i)
        {
            if(i < 1)
            {
                return sumArray[i];
            }
            else
            {
                return sumArray[i] + findSum(sumArray, i-1);
            }
        }
    }
    

    Here you go. There are many errors in this code. You should really go back and learn the basics. The first thing is that the array needs to be defined with [] brackets. Next you need to run the for loop in your sum method lenght-1 times.

    I would suggest to revise how to implement arrays and how to use return.