Task 3 Write a program that takes an input file and produces
Task 3: Write a program that takes an input file and produces the desired results.
--------------------------------------------------------------------------------------------------------------------------
****ColumnName.java****
--------------------------------------------------------------------------------------------------------------------------
****nba.txt****
http://vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/assignments/assignment9/nba.txt
--------------------------------------------------------------------------------------------------------------------------
****weather1.txt****
--------------------------------------------------------------------------------------------------------------------------
****question****
File ColumnName.java contains an incomplete program, that prints the name of a column in 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.
Complete that program, by defining a columnName 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 the name of the specified column in the specified CSV file. If the column is invalid (less than 0 or too high) the function should return null.
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 CSV files you can test your program with are nba.txt and weather1.txt.
An example run of the complete program is shown below. In this example run, file \"not_there.txt\" does not exist, and we use the files nba.txt and weather1.txt.
--------------------------------------------------------------------------------------------------------------------------
****example output****
--------------------------------------------------------------------------------------------------------------------------
Solution
ColumnName.java
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
public class ColumnName
{
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: \");
String name = columnName(filename, column);
if (name == null)
{
System.out.printf(\"Failed to extract a valid column name for column %d of %s\ \ \",
column, filename);
}
else
{
System.out.printf(\"In file %s, column %d is \\\"%s\\\"\ \ \",
filename, column, name);
}
}
}
public static String columnName(String filename, int column){
String name = null;
try{
Scanner scan = new Scanner(new File(filename));
String s = scan.nextLine();
if(s.split(\",\").length > column){
name = s.split(\",\")[column];
}
}
catch(FileNotFoundException e){
System.out.println(\"Inout file \"+filename+\" does not exist\");
}
return name;
}
}
Output:
Enter a filename (or q to quit): D:\\\\weather1.txt
Enter a column: 0
In file D:\\\\weather1.txt, column 0 is \"Date\"
Enter a filename (or q to quit): D:\\\\weather1.txt
Enter a column: 1
In file D:\\\\weather1.txt, column 1 is \"Temperature High\"
Enter a filename (or q to quit): D:\\\\weather1.txt
Enter a column: 2
In file D:\\\\weather1.txt, column 2 is \"Temperature Low\"
Enter a filename (or q to quit): D:\\\\weather1.txt
Enter a column: 3
In file D:\\\\weather1.txt, column 3 is \"Rain\"
Enter a filename (or q to quit): D:\\\\weather1.txt
Enter a column: 4
Failed to extract a valid column name for column 4 of D:\\\\weather1.txt
Enter a filename (or q to quit): q
Exiting...


