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

Part II: Processing a CSV line (10pts) Introduction: String processing and manip

ID: 3772160 • Letter: P

Question

Part II: Processing a CSV line (10pts)

Introduction: String processing and manipulation is an important part of scientific computing. Many data sources store their information in plaintext which can be read in as strings. The data is separated by delimiters.

For example a CSV (comma separated values) file may have entries like: Name, Age, ID where Age and ID are written into the file as text versions of numbers and the data is separated by commas.

Todo: Write a program that gets a CSV line from a user (via the command line). Compute the sum of the numbers and display that sum to the command line.

Hints: Find all of the commas and for each of them get a substring using that comma’s location You need a start and stop location to get substrings Remember how to get sub-matrices/sub-vectors? You may need to treat the first value (before the first comma) and the last one (after the last comma) “specially”. If the substring is a number, add it to your sum

Example:

Input: 4, 5, 6, 0

Output: 15

Input: 4, dkf, 8, fff.0

Output: 12

Input: a b c 5

Output: 0

Explanation / Answer

//input file:input.csv

//4,5,6,ab

//------------------------------------------------

//Program:Driver.java


package javaprogram;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;


public class Driver {

public static void main(String[] args) throws FileNotFoundException, IOException {
String csvFile = args[0]; // reading first argument from command line
//create BufferedReader to read csv file
BufferedReader br = new BufferedReader(new FileReader(csvFile));
String line;
StringTokenizer st = null;
//read comma seperated file
line=br.readLine();
System.out.println(line);
int total=0;
st = new StringTokenizer(line, ",");
while(st.hasMoreTokens())
{
String ip=st.nextToken();
if(ip.matches(".*\d.*"))
total+=Integer.parseInt(ip);
}
System.out.println(total);   
}
}

//-------------------------------------------------

//Output:

Execution @ commandline:

javac Driver.java

java Driver input.csv

4,5,6,ab
15