Task 4: Write a program that takes an input file and produces the desired result
ID: 3773937 • Letter: T
Question
Task 4: Write a program that takes an input file and produces the desired results.
--------------------------------------------------------------------------------------------------------------------------
****ColumnSum.java****
--------------------------------------------------------------------------------------------------------------------------
****nba.txt****
http://vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/assignments/assignment9/nba.txt
--------------------------------------------------------------------------------------------------------------------------
**** weather1.txt****
--------------------------------------------------------------------------------------------------------------------------
****question****
File ColumnSum.java contains an incomplete program, that prints the sum of all values in a specific column of a CSV file. In this program, we make the assumption that the names of all columns are shown at the top line of the CSV file, so when we sum up the values in a column we should exclude the top line.
Complete that program, by defining a columnSum function, that satisfies the following specs:
The function takes two arguments, called filename, column. Argument filename is a string specifying the name of a CSV file. Argument column is an integer, specifying a column in the CSV file. Columns are counted starting at 0, so the leftmost column is column 0.
The function returns (as a double number) the sum of all numbers found in the specified column in the specified CSV file.
As usual, the program should not crash under any circumstances. More specifically:
If the file does not exist, or if the column does not exist, the column sum should be 0.
If, for any row, the specified column is invalid (less than 0 or too high) the function should skip that row and continue to the next.
If, for any row, the specified column does not contain a valid double number (i.e., if Double.parseDouble fails to convert the text in that column into a number) the function should skip that row and continue to the next one.
IMPORTANT: You are NOT allowed to modify in any way the main function. You are free to define and use auxiliary functions. You are also free to use code written in class, posted on the course website, or available on the lecture slides.
--------------------------------------------------------------------------------------------------------------------------
****example output****
Example CSV files you can test your program with are nba.txt and weather1.txt.
An example run of the complete program is shown below.
--------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
ColumnSum.java
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
public class ColumnSum
{
public static int userInteger(String message)
{
Scanner in = new Scanner(System.in);
int result;
while (true)
{
System.out.printf(message);
String s = in.next();
if (s.equals("q"))
{
System.out.printf("Exiting... ");
System.exit(0);
}
try
{
result = Integer.parseInt(s);
}
catch (Exception e)
{
System.out.printf("%s is not a valid number, try again. ", s);
continue;
}
return result;
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while (true)
{
System.out.printf("Enter a filename (or q to quit): ");
String filename = in.next();
if (filename.equals("q"))
{
System.out.printf("Exiting... ");
System.exit(0);
}
int column = userInteger("Enter a column: ");
double sum = columnSum(filename, column);
System.out.printf("In file %s, column %d has sum %.2f ",
filename, column, sum);
}
}
public static double columnSum(String filename, int column){
double sum = 0;
try{
Scanner scan = new Scanner(new File(filename));
while(scan.hasNextLine()){
String s = scan.nextLine();
sum = sum + Double.parseDouble(s.split(",")[1]);
}
}
catch(FileNotFoundException e){
System.out.println("Inout file "+filename+" does not exist");
}
return sum;
}
}
Output:
Enter a filename (or q to quit): D:\weather1.txt
Enter a column: 1
In file D:\weather1.txt, column 1 has sum 1177.00
Enter a filename (or q to quit): q
Exiting...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.