Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

More Books

Students also viewed these Databases questions

Question

4. Use open and closed questions appropriately.

Answered: 1 week ago

Question

2. How were various roles filled?

Answered: 1 week ago