0

I am writing a program and want the program to not loop and request another search pattern if the search pattern (word) contains any non alpha numeric characters.

I have setup a Boolean word to false and an if statement to change the Boolean to true if the word contains letters or numbers. Then another if statement to allow the program to execute if the Boolean is true.

My logic must be off because it is still executing through the search pattern if I simply enter "/". The search pattern cannot contain any non alpha numeric characters to include spaces. I am trying to use Regex to solve this problem.

Sample problematic output:

Please enter a search pattern: /
Line number 1
this.a 2test/austin
            ^

Line number 8
ra charity Charityis 4 times a day/a.a-A
                                  ^

Here is my applicable code:

while (again) {

   boolean found = false;
   System.out.printf("%n%s", "Please enter a search pattern: ", "%n");
   String wordToSearch = input.next();

   if (wordToSearch.equals("EINPUT")) {
      System.out.printf("%s", "Bye!");
      System.exit(0);
   }

   Pattern p = Pattern.compile("\\W*");
   Matcher m = p.matcher(wordToSearch);

   if (m.find())
      found = true;

   String data;
   int lineCount = 1;

   if (found = true) {
      try (FileInputStream fis = 
                 new FileInputStream(this.inputPath.getPath())) {
         File file1 = this.inputPath;
         byte[] buffer2 = new byte[fis.available()];
         fis.read(buffer2);
         data = new String(buffer2);
         Scanner in = new Scanner(data).useDelimiter("\\\\|[^a-zA-z0-9]+");
         while (in.hasNextLine()) {

            String line = in.nextLine();

            Pattern pattern = Pattern.compile("\\b" + wordToSearch + "\\b");
            Matcher matcher = pattern.matcher(line);

            if (matcher.find()) {
               System.out.println("Line number " + lineCount);
               String stringToFile = f.findWords(line, wordToSearch);
               System.out.println();
            }
            lineCount++;
         }
      }
   }
}
pirho
  • 11,565
  • 12
  • 43
  • 70
Austin
  • 51
  • 1
  • 10

4 Answers4

2

Stop reinventing the wheel.

Read this: Apache StringUtils, Focus on isAlpha, isAlphanumeric, and isAlphanumericSpace

One of those is likely to provide the functionality you want.

DwB
  • 37,124
  • 11
  • 56
  • 82
  • 1
    Or use any other already invented wheels in https://stackoverflow.com/questions/8248277/how-to-determine-if-a-string-has-non-alphanumeric-characters – Bedla Dec 04 '17 at 20:07
  • I would like to avoid importing Apache. The link provided by Bedla an answer with a method that I incorporated into the program and now it is functioning. I am upvoting DwB's answer since I did not specify this desire in my question. – Austin Dec 05 '17 at 00:05
1

Create a separate method to call the String you are searching through:

public boolean isAlphanumeric(String str)
{
    char[] charArray = str.toCharArray();
    for(char c:charArray)
    {
        if (!Character.isLetterOrDigit(c))
            return false;
    }
    return true;
}

Then, add the following if statement to the above code prior to the second try statement.

if (isAlphanumeric(wordToSearch) == true)
Austin
  • 51
  • 1
  • 10
1

Well since no one posted REGEX one, here you go:

package com.company;


public class Main {

    public static void main(String[] args) {

       String x = "ABCDEF123456";
       String y = "ABC$DEF123456";

       isValid(x);
       isValid(y);

    }

    public static void isValid(String s){

        if (s.matches("[A-Za-z0-9]*"))
            System.out.println("String doesn't contain non alphanumeric characters !");
        else
            System.out.println("Invalid characters in string !");
    }
}
whatamidoingwithmylife
  • 1,119
  • 1
  • 16
  • 35
0

Right now, what's happening is if the search pattern contains non alphanumeric characters, then do the loop. This is because found = true when the non alphanumeric characters are detected.

if(m.find())
    found = true;

What it should be:

if(!m.find())
    found = true;

It should be checking for the absence of nonalphanumeric characters.

Also, the boolean flag can just be simplified to:

boolean found = !m.find();

You don't need to use the if statement.

a.deshpande012
  • 715
  • 7
  • 18