Added trim to all user input

This commit is contained in:
Keith Armstrong 2025-01-19 19:31:11 -05:00
parent 2a233bc5d3
commit 33dc0592f3
2 changed files with 24 additions and 16 deletions

View File

@ -15,8 +15,9 @@ import java.util.regex.Pattern;
public class fileProcessor { public class fileProcessor {
public static String readFirstLine(String thisFile) throws IOException { public static String readFirstLine(String thisFile) throws IOException {
File file = new File(thisFile); String myFile = thisFile.trim();
boolean isValid = validFile(thisFile); File file = new File(myFile);
boolean isValid = validFile(myFile);
if ( !isValid ) if ( !isValid )
{ {
@ -36,23 +37,25 @@ public class fileProcessor {
} }
public static Boolean getFile(String myFile) { public static Boolean getFile(String thisFile) {
if (myFile == null) { if (thisFile == null) {
return false; return false;
} }
File thisFile = new File(myFile); String myFile = thisFile.trim();
if (thisFile.exists()) { File checkFile = new File(myFile);
if (checkFile.exists()) {
return true; return true;
} }
return false; return false;
} }
public static long getSize(String myFile) public static long getSize(String thisFile)
{ {
try try
{ {
String myFile = thisFile.trim();
File varFile = new File (myFile); File varFile = new File (myFile);
return varFile.length(); return varFile.length();
} }
@ -62,9 +65,10 @@ public class fileProcessor {
} }
} }
public static String replaceInFile(String myFile, Map hashMap) { public static String replaceInFile(String thisFile, Map hashMap) {
File thisFile = new File(myFile); String myFile = thisFile.trim();
if (!thisFile.exists()) { File checkFile = new File(myFile);
if (!checkFile.exists()) {
return null; return null;
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -97,8 +101,9 @@ public class fileProcessor {
public static boolean fileHasText(String thisFile, String word) throws FileNotFoundException public static boolean fileHasText(String thisFile, String word) throws FileNotFoundException
{ {
String myFile = thisFile.trim();
boolean flag = false; boolean flag = false;
Scanner sc2 = new Scanner(new FileInputStream(thisFile)); Scanner sc2 = new Scanner(new FileInputStream(myFile));
while(sc2.hasNextLine()) { while(sc2.hasNextLine()) {
String line = sc2.nextLine(); String line = sc2.nextLine();
if( line.indexOf(word) != -1 ) if( line.indexOf(word) != -1 )
@ -124,7 +129,8 @@ public class fileProcessor {
public static boolean validFile(String thisFile) public static boolean validFile(String thisFile)
{ {
Path path = Paths.get(thisFile); String myFile = thisFile.trim();
Path path = Paths.get(myFile);
boolean result = true; boolean result = true;
long total = 0; long total = 0;
InputStreamReader isr = null; InputStreamReader isr = null;
@ -138,7 +144,7 @@ public class fileProcessor {
decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onMalformedInput(CodingErrorAction.REPORT);
try { try {
isr = new InputStreamReader(new FileInputStream(thisFile), decoder); isr = new InputStreamReader(new FileInputStream(myFile), decoder);
while ((chars = isr.read(buf)) >= 0) while ((chars = isr.read(buf)) >= 0)
total += chars; total += chars;
} catch (CharacterCodingException ex) { } catch (CharacterCodingException ex) {
@ -156,9 +162,11 @@ public class fileProcessor {
return result; return result;
} }
public static long numberOfLines(String fileName) public static long numberOfLines(String thisFile)
{ {
Path path = Paths.get(fileName); String myFile = thisFile.trim();
Path path = Paths.get(myFile);
long lines = 0; long lines = 0;
try try

View File

@ -21,7 +21,7 @@ public class userVerify
while (true) while (true)
{ {
System.out.print(varPrompt); System.out.print(varPrompt);
varFileName = myObj.nextLine(); varFileName = myObj.nextLine().trim();
File thisFile = new File(varFileName); File thisFile = new File(varFileName);
boolean isValidFile = fileProcessor.validFile(varFileName); boolean isValidFile = fileProcessor.validFile(varFileName);
if ( thisFile.exists() && isValidFile ) if ( thisFile.exists() && isValidFile )