0

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?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • For capBool did you mean `[A-Z]` or `[A-Z]*` ? – vikingsteve Feb 10 '15 at 12:55
  • 1
    Learn to debug it, you can answer most of your own questions there. – weston Feb 10 '15 at 12:56
  • It seems your regular expressions are insufficient. Can you add example lines to your question, and tell us which of them is supposed to result in `true` in each of the booleans? – RealSkeptic Feb 10 '15 at 12:56
  • have edited my question @RealSkeptic . I don't pretend to be a regex pro, so i'm not surprised if they're the issue. –  Feb 10 '15 at 13:03

1 Answers1

1

matches means what it looks like it does - checks whether the whole string matches your regex. That however is not what you want most probably, judging by the way you wrote those regexes - what you seem to be looking for is a kind of regexy contains.

The sad part is however, there is no alternative pretty syntax to do what you want - see How to use regex in String.contains() method in Java. You need to go and rewrite your regexes.

And it shouldn't be that hard. Just surround your ready expressions with .* to indicate that what you're looking for can be preceeded or followed by any number of characters - i.e. [A-Z] can be changed to .*[A-Z].*

Community
  • 1
  • 1
Deltharis
  • 2,320
  • 1
  • 18
  • 29
  • Many thanks! Works perfect , and taught me something useful about regex - cheers! –  Feb 10 '15 at 13:20
  • Note that this won't work if the string contains multiple lines, because `.` does not match a newline character. To get around this, use `[\\S\\s]*[A-Z][\\S\\s]*`. – jsheeran Sep 21 '15 at 08:45
  • @jsheeran that is a good point, however note the question - he tests "each line", which by definition excludes the possibility of having multiple lines within the string. – Deltharis Sep 21 '15 at 08:54