Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Could someone help me with arrays & recursion??

807600Nov 18 2007 — edited Nov 19 2007
Hi, I'm new here and I'm having trouble.

I have to write a java application which uses recursion to find the maximal contiguous sum in a list of integers.

The input is read from a text file where each line represents a list containing the following integers:

For example, it might contain

7 -2 -4 30 15 -7 -5 1000
2 -50 100
6 1000 -2000 900 2800 -2900 2801
4 100 -10 5 8
4 100 -50 5 8

And for the output, for each line of data read, I have to display the largest sum of consecutive integers in the list followed by the list itself. For example for the lists above, my output would be:

(the first number is the largest sum of consecutive integers and the rest are the list of integers)

1033 -2 -4 30 15 -7 -5 1000
100 -50 100
3700 1000 -2000 900 2800 -2900 2801
103 100 -10 5 8
100 100 -50 5 8

I have to use a loop to read each line of data until the end of file is reached. I was able to do that.

import java.util.Scanner;
import java.io.*;

public class RecursionApp 
{
  public static void main(String[] args) throws IOException
  {
    String line;
    Scanner fileScan;
    
    fileScan = new Scanner(new File("src\\List.dat"));
    
    while (fileScan.hasNext())
    {
      line = fileScan.nextLine();
      
      System.out.println(line);
     
    }
  }
}
Now this is the part that I'm having trouble with. On each pass of the loop I have to create an array of list-size (1st number on the line) to hold the list of numbers read (from the rest of the line). Then invoke a method that returns the largest contiguous sum for this list. And display the sum and the list (as above).

Any help will be greatly appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 17 2007
Added on Nov 18 2007
7 comments
259 views