In: Computer Science
Language: Java
I have written this code but not all methods are running. First method is running fine but when I enter the file path, it is not reading it.
Directions
The input file must be read into an input array and data validated from the array. Input file format (500 records maximum per file): comma delimited text, should contain 6 fields: any row containing exactly 6 fields is considered to be invalid
Purpose of this code is to : Read the file from where it is place, data validate it for correctness, manipulate it into a slightly different format and write a new version of all VALID rows to a new file placed in a standard location. Create an error/suspense file that contains only the rows that contain errors and place it into a standard location.Teammate/mentor and you have decided to code the application using the main method to only call other methods to do the work.
Code
import java.io.*;
import java.text.MessageFormat;
import java.util.*;
public class Students {
static public final int ARRAY_SIZE = 500;
static public final int REQUIRED_TOKEN_NUMBER = 6;
static public File inputFile;
static public File goodFile = new File("c:" + File.separator + "student" +
File.separator + "students.dat");
static public File errorFile = new File("c:" + File.separator + "student" +
File.separator + "studentSuspense.dat");
static public BufferedReader inStream;
static public BufferedReader inFileStream;
static public BufferedWriter goodFileOut, errorFileOut;
static public String inputArray[];
public static void main(String args[]) {
getFileNameAndPath();
openInputFile();
fillInputArray();
openGoodFile();
openErrorFile();
validateData();
closeOutputFiles();
System.exit(0);
}
public static void getFileNameAndPath() {
try{
inStream = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please, enter file path: ");
String filePath = inStream.readLine().trim();
inputFile = new File(filePath);
}
catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
public static void openInputFile() {
try {
inFileStream = new BufferedReader(new FileReader(inputFile));
}
catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
private static void fillInputArray() {
try {
inputArray = new String[ARRAY_SIZE];
int counter = 0;
String line = null;
while ((line = inFileStream.readLine()) != null) {
inputArray[counter++] = line;
}
inFileStream.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
private static void openGoodFile() {
try {
goodFileOut = new BufferedWriter(new FileWriter(goodFile));
}
catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
private static void openErrorFile() {
try {
errorFileOut = new BufferedWriter(new FileWriter(errorFile));
}
catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
private static void validateData() {
for(int i = 0; i<inputArray.length; i++) {
String line = inputArray[i];
if (line == null) {
break;
}
String[] parts = line.split(",");
int age = 0;
double score = 0.0;
if (parts.length != REQUIRED_TOKEN_NUMBER) {
writeLineToErrorFile(line);
continue;
}
try {
age = Integer.parseInt(parts[4]);
}
catch (NumberFormatException e) {
writeLineToErrorFile(line);
continue;
}
try {
score = Double.parseDouble(parts[5]);
}
catch (NumberFormatException e) {
writeLineToErrorFile(line);
continue;
}
writeLineToGoodFile(parts[0], parts[1], parts[2], parts[3], age, score);
}
}
public static void writeLineToErrorFile(String badLine) {
try {
errorFileOut.write(badLine + System.lineSeparator());
}
catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
public static void writeLineToGoodFile(String lName, String fName,
String mName, String id, int age, double examScore) {
String newLine = MessageFormat.format("{0}, {1}|{0}|{1}|{2}|{3}|{4}|{5}",
lName, fName, mName,id, age, String.format("%.1f", examScore));
try {
goodFileOut.write(newLine + System.lineSeparator());
}
catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
public static void closeOutputFiles() {
try {
goodFileOut.close();
errorFileOut.close();
}
catch (IOException e) {
System.err.println(e.getMessage());
System.exit(-1);
}
}
}
import java.io.*;
import java.text.MessageFormat;
import java.util.*;
public class Students {
static public final int ARRAY_SIZE = 500;
static public final int REQUIRED_TOKEN_NUMBER = 6;
static public File inputFile;
//good file path should be present otherwise getting error with
file not found
static public File goodFile = new File("c:" + File.separator +
"student"
+ File.separator + "students.dat");
//also errorfile should be present on provided path otherwise
exception will occur
static public File errorFile = new File("c:" + File.separator +
"student"
+ File.separator + "studentSuspense.dat");
static public BufferedReader inStream;
static public BufferedReader inFileStream;
static public BufferedWriter goodFileOut, errorFileOut;
static public String inputArray[];
public static void main(String args[]) {
getFileNameAndPath();
openInputFile();
fillInputArray();
openGoodFile();
openErrorFile();
validateData();
closeOutputFiles();
//write to console if there is no any exception or error
System.out.println("File validated successfully...");
System.exit(0);
}
public static void getFileNameAndPath() {
try {
//get file path from user with file name
inStream = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Please, enter file path: ");
String filePath = inStream.readLine().trim();
inputFile = new File(filePath);
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
public static void openInputFile() {
try {
//read the input file
inFileStream = new BufferedReader(new FileReader(inputFile));
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
private static void fillInputArray() {
try {
//read file and convert it into array
inputArray = new String[ARRAY_SIZE];
int counter = 0;
String line = null;
while ((line = inFileStream.readLine()) != null) {
inputArray[counter++] = line;
}
inFileStream.close();
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
private static void openGoodFile() {
try {
goodFileOut = new BufferedWriter(new FileWriter(goodFile));
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
private static void openErrorFile() {
try {
errorFileOut = new BufferedWriter(new FileWriter(errorFile));
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
private static void validateData() {
//validate the each line if data contains exactly 6 fields then it
is valid if not then invalid
for (int i = 0; i < inputArray.length; i++) {
String line = inputArray[i];
if (line == null) {
break;
}
String[] parts = line.split(",");
int age = 0;
double score = 0.0;
if (parts.length != REQUIRED_TOKEN_NUMBER) {
writeLineToErrorFile(line);
continue;
}
try {
age = Integer.parseInt(parts[4]);
} catch (NumberFormatException e) {
writeLineToErrorFile(line);
continue;
}
try {
score = Double.parseDouble(parts[5]);
} catch (NumberFormatException e) {
writeLineToErrorFile(line);
continue;
}
//write data to goodfile
writeLineToGoodFile(parts[0], parts[1], parts[2], parts[3], age,
score);
}
}
public static void writeLineToErrorFile(String badLine) {
try {
errorFileOut.write(badLine + System.lineSeparator());
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
public static void writeLineToGoodFile(String lName, String
fName,
String mName, String id, int age, double examScore) {
String newLine = MessageFormat.format("{0},
{1}|{0}|{1}|{2}|{3}|{4}|{5}",
lName, fName, mName, id, age, String.format("%.1f",
examScore));
try {
goodFileOut.write(newLine + System.lineSeparator());
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
public static void closeOutputFiles() {
try {
//close all open files
goodFileOut.close();
errorFileOut.close();
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(-1);
}
}
}
Note: path should be contain filename and
extension
//output
Please, enter file path:
C:\Users\PhoenixZone\Desktop\testfile.txt
File validated successfully...
//input file data (testfile.txt)
Dinesh,Ubale,First Year,Computer
Engineering,lo,63.2
Dinesh,SDE,Second Year,Computer Engineering,23,65.8
John,Smith,Third Year,Computer Engineering,25,80.36
Dinsh,Pawar,First Year,Mechanical Engineering,26,70.5
Dnew,Nimbhore,First Year,Civil Engineering,27,46.2
Din,UHY,Fourth Year,E&TC Engineering,28,66.0
//students.dat file data
Dinesh, SDE|Dinesh|SDE|Second Year|Computer
Engineering|23|65.8
John, Smith|John|Smith|Third Year|Computer
Engineering|25|80.4
Dinsh, Pawar|Dinsh|Pawar|First Year|Mechanical
Engineering|26|70.5
Dnew, Nimbhore|Dnew|Nimbhore|First Year|Civil
Engineering|27|46.2
Din, UHY|Din|UHY|Fourth Year|E&TC
Engineering|28|66.0
//studentSuspense.dat file data
Dinesh,Ubale,First Year,Computer
Engineering,lo,63.2