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.