Hey I am trying to practice some recursive function writing for a class I am taking and I am writing this code and I pull up an error that says ".class" expected in the bold, underlined line!

import java.util.*;

/* This class is one in which the program will take the sum
 * of a set of integers in an array
 */
public class ArraySum
{
  
/* This method is one in which the function will take the
 * integer values in an array and perform the sum of them
 * from the first parameter to the last parameter inclusive
 */
  public static int array_sum(int A[], int first, int last)
  {
    if(first == last)
    {
      return A[first];
    }
    else
    {
      [B][U]return A[last] + array_sum(A[],first,last-1);[/U][/B]
    }
  } // end array_sum method
  
  public static void main(String[] args)
  {
    for(int first = 0; first>=0; i++)
    {
      //Gather the integers
      System.out.println("Next Integer:");
      Scanner sc2 = new Scanner(System.in);
      first = sc2.nextInt();
    }

    // Print solution
    System.out.println(array_sum(A[1000],3,0));
  } //end main method
} // end ArraySum class

Recommended Answers

All 8 Replies

Drop the [] from "A[]" in this portion array_sum(A[],first,last-1) , you only need those in the declaration, not when using the variable. The array itself is merely "A".

OK so now I get an error message that says

File: C:\Users\Owner\Downloads\Dr Java\Stuff\bookClasses\Hmwrk2.java [line: 41]
Error: C:\Users\Owner\Downloads\Dr Java\Stuff\bookClasses\Hmwrk2.java:41: cannot find symbol
symbol : variable A
location: class Hmwrk2

I altered the code a bit to fix one other error involving the index variable and so here is the main method again!

public static void main(String[] args)
  {
    
    for(int first = 0; first>=0;)
    {
      //Gather the integers
      System.out.println("Next Integer:");
      Scanner sc2 = new Scanner(System.in);
      first = sc2.nextInt();
    }

    // Print solution
    [B][U]System.out.println(array_sum(A[1000],3,0));[/U][/B]
  } //end main method

Do you see any variable named "A" declared in your main() method?

OK so i fixed it and i got it to compile but now i am having trouble getting it to do wut i want it to do! The following is the task I am given!

Write a recursive function that will find the sum of all the elements in an array A, from position first to last inclusive. This function is a lot like the maxArray function, which you saw in the book and the lab. The definition of this function should start with
int array_sum(int A[], int first, int last)

It returns the sum A[first]+A[first+1]...+A[last]. Write a "main" function, which reads integers from the console and stores them in an array. Positive integers only -- stop when the user inputs a negative number. The main function calls your recursive function to find the sum, giving it the array and the positions where the user's data starts and ends. Assume at most 1000 inputs. The main function finally prints the answer. The user should see this:
input integer:5
input integer:4
input integer:9
input integer:-1
sum is 18

This is my new main method code:

public static void main(String[] args)
  {
    int[] A = new int[1000];
    int first = 0;
    
    //Gather the first integer
    System.out.println("Next Integer:");
    Scanner sc2 = new Scanner(System.in);
    first = sc2.nextInt();
      
    if(first<0)
    {
      // Print solution
      System.out.println(array_sum(A,3,0));
    } // end if
    
    else
    {
      //Gather the integers
      System.out.println("Next Integer:");
      first = sc2.nextInt();
    } // end else
   
    // Print solution
      System.out.println(array_sum(A,3,0));
      
  } //end main method

OK so I got it working now to go through all of the integers but once I put in the -1 it comes up with:

java.lang.ArrayIndexOutOfBoundsException: -1
at Hmwrk2.array_sum(Hmwrk2.java:26)
at Hmwrk2.array_sum(Hmwrk2.java:26)
at Hmwrk2.main(Hmwrk2.java:48)

Now, as it seems, my only issue left is to figure out maybe some sort of if else statement within the for loop?

public static void main(String[] args)
  {
    int[] A = new int[1000];
    int i = 0;
    for(int n = 0; n >= 0; i++)
    {
    
      //Gather the integers
      System.out.println("Next Integer:");
      Scanner sc2 = new Scanner(System.in);
      n = sc2.nextInt();
      
      //Apply integer to next indexed location
      A[i] = n;
      
    } //end for
   
    // Print solution
      System.out.println(array_sum(A,3,0));
      
  } //end main method

any luck?

you are looking in the wrong place, it is falling over in the array_sum method. can you paste it?

public static int array_sum(int A[], int first, int last)
  {

    if(first == last)
    {
      return A[first];
    }
    else
    {
      return A[last] + array_sum(A,first,last-1);
    }
  } // end array_sum method

  public static void main(String[] args)
  {
    int[] A = new int[1000];
    int i = 0;
    for(int n = 0; n >= 0; i++)
    {

      //Gather the integers
      System.out.println("Next Integer:");
      Scanner sc2 = new Scanner(System.in);
      n = sc2.nextInt();

      //Apply integer to next indexed location
      A[i] = n;

    } //end for

    // Print solution
      System.out.println(array_sum(A,3,0));

  } //end main method
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.