Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I want the user to beable to enter the input exactly like that and not have to e

ID: 3876734 • Letter: I

Question

I want the user to beable to enter the input exactly like that and not have to enter the size of the array. I want this question answered in Java.

Write a program to determine the largest sum of contiguous integers in a sequence. Contiguous means that the integers are adjacent to each other. Take -4, 2, -5, 0, 3 as an example. For this example the largest sum would be 3 and was obtained from the subsequence 0, 3. Input: Your program should read lines of text from standard input. Each line will contain a sequence of comma-separated integers Output: For each line of input, print to standard output the sum of the largest contiguous subsequence in each sequence, one integer per line. In other words, of all the possible contiguous subsequences for a given set, find the one with the largest sum, and print that sum. Test 1 Test input -10, 2, 3, -2, 0, 5, -15 Expected Output

Explanation / Answer

import java.util.Scanner; import java.util.StringTokenizer; public class LargestContiguousSum { public static void main(String args[]){ String integers; Scanner scanner = new Scanner(System.in); // Take input from users, comma seperated integers System.out.print("Enter comma(,) seperated integers as input: "); integers = scanner.nextLine(); // Remove all the white spaces entered in the input integers = integers.replaceAll("\s",""); // Break the entered tokens into tokens StringTokenizer tokens = new StringTokenizer(integers, ","); // Convert tokens into an array int arr[] = new int[tokens.countTokens()]; int index = 0; while (tokens.hasMoreTokens()) { arr[index++] = Integer.parseInt(tokens.nextToken()); } // Calculate the sum int sum = 0; int max_sum = 0; // Loop until last element of the array for(index = 0; index
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote