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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started