Embed presentation
Download to read offline
![Change given Java code to return the sum of a comma separated integer list scanned from input
file. Please do NOT use any BufferedReader methods, this needs to be done with scanner. and
ONLY modify the body of the public int Problem method.
The code works fine for a file of ints, I just need it adjusted to meet the new input scenario which
will only be txt files containing one line of ints seperated by commas. Explain as much as
possible, I am new and need to understand what and why changes were made.
package Mod4;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Problem1 {
public static void main(String[] args) {
new Problem1();
}
public Problem1() {
int problem1Solution = Problem(new File("Problem1.txt"));
System.out.println(problem1Solution);
}
public int Problem(File file) {
//ONLY MODIFY CODE BELOW HERE
int sum = 0;
try {
Scanner scanFile = new Scanner(file);
while (scanFile.hasNextLine()) {
String fileline = scanFile.nextLine();
Scanner lineInput = new Scanner(fileline);
while (lineInput.hasNextInt()) {
sum += lineInput.nextInt();
}
lineInput.close();
}
scanFile.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
return sum;
}
}](https://image.slidesharecdn.com/changegivenjavacodetoreturnthesumofacommaseparatedinteger-230314035242-f1ed6a31/85/Change-given-Java-code-to-return-the-sum-of-a-comma-separated-integer-pdf-1-320.jpg)

The document outlines a Java code modification task that requires changing a method to compute the sum of integers from a comma-separated list in a text file. It specifies the use of the Scanner class instead of BufferedReader methods and focuses on adjusting the implementation within the public int problem method. The user seeks a clear explanation of the changes to enhance their understanding of the code.
![Change given Java code to return the sum of a comma separated integer list scanned from input
file. Please do NOT use any BufferedReader methods, this needs to be done with scanner. and
ONLY modify the body of the public int Problem method.
The code works fine for a file of ints, I just need it adjusted to meet the new input scenario which
will only be txt files containing one line of ints seperated by commas. Explain as much as
possible, I am new and need to understand what and why changes were made.
package Mod4;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Problem1 {
public static void main(String[] args) {
new Problem1();
}
public Problem1() {
int problem1Solution = Problem(new File("Problem1.txt"));
System.out.println(problem1Solution);
}
public int Problem(File file) {
//ONLY MODIFY CODE BELOW HERE
int sum = 0;
try {
Scanner scanFile = new Scanner(file);
while (scanFile.hasNextLine()) {
String fileline = scanFile.nextLine();
Scanner lineInput = new Scanner(fileline);
while (lineInput.hasNextInt()) {
sum += lineInput.nextInt();
}
lineInput.close();
}
scanFile.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
return sum;
}
}](https://image.slidesharecdn.com/changegivenjavacodetoreturnthesumofacommaseparatedinteger-230314035242-f1ed6a31/85/Change-given-Java-code-to-return-the-sum-of-a-comma-separated-integer-pdf-1-320.jpg)
