Fixed an error handling gotcha

This commit is contained in:
Keith Armstrong 2025-01-03 11:30:49 -05:00
parent 659493c090
commit 96c0742b5e
2 changed files with 30 additions and 15 deletions

View File

@ -11,6 +11,12 @@ public class fileProcessor {
public static String readFirstLine(String thisFile) throws IOException {
File file = new File(thisFile);
Boolean isValid = validFile(thisFile);
if ( !isValid )
{
return "";
}
if (file.exists()) {
FileReader fr = new FileReader(file);
LineNumberReader ln = new LineNumberReader(fr);

View File

@ -1,6 +1,5 @@
package com.belkast.soap;
import org.apache.commons.csv.CSVException;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
@ -17,7 +16,7 @@ public class userVerify
String varFileName;
while (true) {
System.out.print("Please enter the name of the CSV file to verify : ");
System.out.print("Please enter the CSV filename to validate : ");
varFileName = myObj.nextLine(); // Read user input
File thisFile = new File(varFileName);
if (thisFile.exists())
@ -36,8 +35,8 @@ public class userVerify
valueMap.put("n", false);
while (true) {
System.out.print("Invalidate the line if empty column value (Y/n) : ");
String varAnswer = myObj.nextLine(); // Read user input
System.out.print("Invalidate the line on empty column values? (Y/n) : ");
String varAnswer = myObj.nextLine();
if (varAnswer.toUpperCase().equals("Y") || varAnswer.toUpperCase().equals("N") || varAnswer == "")
{
@ -47,8 +46,8 @@ public class userVerify
}
System.out.println();
System.out.println("CSV input file : " + varFileName);
System.out.println("CSV block on empty : " + varUserBlockEmpty);
System.out.println("CSV filename to validate : " + varFileName);
System.out.println("CSV invalidate on empty : " + varUserBlockEmpty);
ArrayList thisCSV = readCSV(varFileName, true, varUserBlockEmpty);
System.out.println();
@ -79,7 +78,7 @@ public class userVerify
ArrayList finalArray = new ArrayList();
File varFile = new File (thisFile);
if (!varFile.exists()) {
System.out.println(varFile + " does not exist. Please enter a valid filename!");
System.out.println("CSV failure error : File not found. Please enter a valid filename!");
System.exit(0);
}
@ -94,7 +93,7 @@ public class userVerify
String varHeader = new String();
varHeader = fileProcessor.readFirstLine(thisFile);
if (varHeader.length() == 0) {
System.out.println(thisFile + " : the CSV file appears to be missing a valid header!");
System.out.println("CSV failure error : Header line not found!");
return finalArray;
}
@ -109,13 +108,19 @@ public class userVerify
.build();
Iterable<CSVRecord> records = csvFormat.parse(in);
int thisHeaderLength = varHeader.split(",").length;
debugProcessor.writer(fromUser, "CSV header length : " + thisHeaderLength);
debugProcessor.writer(fromUser, "CSV header tokens : " + varHeader);
debugProcessor.writer(fromUser, "");
for (CSVRecord record : records) {
readNum++;
lineNum = record.getParser().getCurrentLineNumber();
boolean isValid = record.isConsistent();
int thisSize = record.size();
String thisLine = Files.readAllLines(Paths.get(thisFile)).get(Math.toIntExact(lineNum - 1));
Boolean validValues = true;
if (isValid)
@ -129,22 +134,26 @@ public class userVerify
if (blockEmpty && (pair.getValue() == null || thisVal.isEmpty()))
{
validValues = false;
debugProcessor.writer(fromUser, "!! line " + lineNum + " [failed] : the " + pair.getKey() + " token value is empty");
debugProcessor.writer(fromUser, "!! line " + lineNum + " [failed] : check for empty token value [" + pair.getKey() + "]");
}
}
}
Map<String, String> thisMap = record.toMap();
if (isValid && validValues)
{
debugProcessor.writer(fromUser, "## line " + lineNum + " [passed]");
finalArray.add(record.toMap());
finalArray.add(thisMap);
}
else
{
invalidNum++;
String thisLine = Files.readAllLines(Paths.get(thisFile)).get(Math.toIntExact(lineNum - 1));
debugProcessor.writer(fromUser, "!! line " + lineNum + " [failed] : " + thisLine);
debugProcessor.writer(fromUser, "!! line " + lineNum + " [failed] : " + record.toMap().toString());
if (thisSize > thisHeaderLength )
{
debugProcessor.writer(fromUser, "!! line " + lineNum + " [failed] : too many tokens or preceding line is wonky [map size is " + thisSize + ", should be " + thisHeaderLength + "]");
}
}
}
}
@ -161,12 +170,12 @@ public class userVerify
{
debugProcessor.writer(fromUser,"");
}
debugProcessor.writer(fromUser, "CSV lines read : " + String.valueOf(readNum));
debugProcessor.writer(fromUser, "CSV lines passed : " + String.valueOf(readNum - invalidNum));
debugProcessor.writer(fromUser, "CSV lines failed : " + invalidNum);
debugProcessor.writer(fromUser, "CSV lines read : " + String.valueOf(readNum));
debugProcessor.writer(fromUser, "CSV lines passed : " + String.valueOf(readNum - invalidNum));
debugProcessor.writer(fromUser, "CSV lines failed : " + invalidNum);
if (exceptionReason != null)
{
debugProcessor.writer(fromUser, "CSV failure error : " + exceptionReason);
debugProcessor.writer(fromUser, "CSV failure error : " + exceptionReason);
}
if (invalidNum > 0)