I'm using a BufferedReader
to read a text file line by line.
I've got a series of three regular expressions to test each line for numbers
, capitals
and special chars
.
If any of them match, I set a boolean
to true.
I want to use these booleans later to output a message to the user telling them the findings of the 'scan'.
However, when I come to use the booleans, they are all still set to false, despite me adding numbers, caps etc into the test text file.
If a line in the text file was, say abC 6
then numBool
and capBool
should return true, but not specialBool
import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
class ReadFile implements Runnable
{
public void run()
{
String line;
Boolean capBool = false;
Boolean numBool = false;
Boolean specialBool = false;
try
{
/* ------------- Get Filename ------------- */
System.out.print("Enter file to scan : ");
BufferedReader fileGet = new BufferedReader(new InputStreamReader(System.in));
String file_name = fileGet.readLine();
fileGet.close();
/* ---------------- Open File ---------- */
BufferedReader br = new BufferedReader(new FileReader(file_name));
/* --------------- Scan file line by line ----------- */
while((line = br.readLine()) != null)
{
if(line.matches("[A-Z]"))
capBool = true;
if(line.matches("[0-9]"))
numBool = true;
if(line.matches("[$&+,:;=?@#|]"))
specialBool = true;
System.out.println(line);
}
br.close(); // close reader
DisplayResults display = new DisplayResults();
String findings = display.displayFindings(capBool, numBool, specialBool);
System.out.print(findings);
}
catch(IOException ex)
{
System.out.println(ex.getMessage() + " - Please check log file for more details");
ex.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException
{
System.setErr(new PrintStream(new FileOutputStream("Exceptions.txt")));
Runnable r = new ReadFile();
Thread th = new Thread(r);
th.start();
}
}
Is this because the booleans are being overwritten somehow when I exit the while loop? Or are they never getting set to true because of a faulty regex method?