Question: I need to write a Java program to determine the largest sum of contiguous integers in a sequence. So if the input is -10, 2,

I need to write a Java program to determine the largest sum of contiguous integers in a sequence. So if the input is -10, 2, 3, -2, 0, 5, -15 the largest sum is 8. If the input is 2,3,-2,-1,10 the largest sum is 12.

I wrote something, but it's not given me the largest sum. Can you fix my code?

import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { FileReader input = new FileReader(args[0]); BufferedReader in = new BufferedReader(input); String line; line = in.readLine(); while(line != null){ summNum(line); line = in.readLine(); } in.close(); System.exit(0); }

public static void summNum(String line){ String[] num = line.split(","); Integer largest = Integer.parseInt(num[0].trim()); Integer scndLargest = 0; for(String s : num){ Integer converted = Integer.parseInt(s.trim()); if(converted > largest){ scndLargest = largest; largest = converted; } } System.out.println(largest+scndLargest); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!