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

For taking input from user, the input should take commas and ignore them : int m

ID: 3551470 • Letter: F

Question

For taking input from user, the input should take commas and ignore them :   

int maxSum;

Scanner scanner = new Scanner (System.in);

int length;

System.out.println("Enter length of the array : ");

length = scanner.nextInt();

System.out.println("Enter the array : ");

  

int a[] = new int[length];

for (int i = 0; i < length; i++) {

a[i] = scanner.nextInt();

}

maxSum = maxSubSum1( a );

System.out.println( "Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd );

maxSum = maxSubSum2( a );

System.out.println( "Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd );

maxSum = maxSubSum3( a );

System.out.println( "Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd );

maxSum = maxSubSum4( a );

System.out.println( "Max sum is " + maxSum );

}

For reading from file, make the following changes in main & import following :

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public static void main(String[] args) {

BufferedReader reader;

int maxSum;

int a[] = new int[8];

String[] parts = new String[8];

try {

reader = new BufferedReader(new FileReader("file.txt"));

String line = null;

while ((line = reader.readLine()) != null) {

parts = line.split("\s");

}

a = new int[parts.length];

int i =0;

for (String part : parts) {

Integer in = Integer.valueOf(part);

a[i] = in;

i++;

}

} catch (FileNotFoundException ex) {

System.out.println("File Not found !");

} catch (IOException ex) {

System.out.println("Error will reading from file !");

}



maxSum = maxSubSum1(a);

System.out.println("Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd);

maxSum = maxSubSum2(a);

System.out.println("Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd);

maxSum = maxSubSum3(a);

System.out.println("Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd);

maxSum = maxSubSum4(a);

System.out.println("Max sum is " + maxSum);

}

The text file should be saved in the project folder named file.txt with following content :

4,-3, 5, -2, -1, 2, 6, -2

Explanation / Answer

For taking input from user, with commas :

. public static void main(String[] args) {


int maxSum;

Scanner scanner = new Scanner(System.in);

int length;

System.out.println("Enter length of the array : ");

length = scanner.nextInt();

System.out.println("Enter the array : ");


int a[] = new int[length];

for (int i = 0; i < length; i++) {

String parts = scanner.next();

String num[] = parts.split("\s*,\s*");

a[i] = Integer.parseInt(num[0]);

}


maxSum = maxSubSum1(a);

System.out.println("Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd);

maxSum = maxSubSum2(a);

System.out.println("Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd);

maxSum = maxSubSum3(a);

System.out.println("Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd);

maxSum = maxSubSum4(a);

System.out.println("Max sum is " + maxSum);

}


For reading from file, with values writen with commas :

public static void main(String[] args) {

  

BufferedReader reader;

int maxSum;

int a[] = new int[8];

String[] parts = new String[8];

try {

reader = new BufferedReader(new FileReader("file.txt"));

String line = null;

while ((line = reader.readLine()) != null) {

parts = line.split("\s");

}

a = new int[parts.length];

int i = 0;

for (String part : parts) {

String[] numPart = part.split("\s*,\s*");

Integer in = Integer.valueOf(numPart[0]);

a[i] = in;

i++;

}

} catch (FileNotFoundException ex) {

System.out.println("File Not found !");

} catch (IOException ex) {

System.out.println("Error will reading from file !");

}



maxSum = maxSubSum1(a);

System.out.println("Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd);

maxSum = maxSubSum2(a);

System.out.println("Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd);

maxSum = maxSubSum3(a);

System.out.println("Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd);

maxSum = maxSubSum4(a);

System.out.println("Max sum is " + maxSum);

}


The file:

4, -3, 5, -2, -1, 2, 6, -2


the change part arre made italic.