1

I am working on a java project and was wondering how I could use a string to determine whether or not the chars in an array are valid. The given string is final String ALLOWED_CHARS = "01".

Say I have 2 char arrays:

char[] valid = {0,0,1,0,0,0,1,1,0,1}
char[] invalid = {0,0,1,0,0,A,1,1,0,1}

What would be an efficient way of determining valid and invalid arrays using the ALLOWED_CHARS string? I assume I am going to need to loop through the array and somehow compare each char to the chars in ALLOWED_CHARS but I'm not sure how to go about doing it.

4
  • 1
    Create Set of valid chars and use contains method for check is character valid or not. Commented Apr 29, 2022 at 4:55
  • Your second char[] is so invalid, it wouldn't even compile… Commented Apr 29, 2022 at 4:57
  • How about checking the ASCII value for each character? Commented Apr 29, 2022 at 4:59
  • @deHaar Yeah sorry, I just wrote it as mainly pseudo code. Commented Apr 29, 2022 at 5:38

2 Answers 2

3

You could form strings from the input character arrays and then use String#matches along with a regex pattern.

final String ALLOWED_CHARS = "01";
String regex = "[" + ALLOWED_CHARS + "]+";

char[] valid = {'0', '0', '1', '0', '0', '0', '1', '1', '0', '1'};
char[] invalid = {'0', '0', '1', '0', '0', 'A', '1', '1', '0', '1'};
String s1 = new String(valid);
String s2 = new String(invalid);

System.out.println(s1 + " valid? " + s1.matches(regex));
System.out.println(s2 + " valid? " + s2.matches(regex));

This prints:

0010001101 valid? true
00100A1101 valid? false
Sign up to request clarification or add additional context in comments.

2 Comments

Would you say this is more efficient than using the contains method as someone else suggested?
You could use a set based method, but the problem there is that it might not scale so well in the event that you have lots of different characters in the blacklist. In that case, for simplicity, I'd rather do this using strings.
0
import java.util.*;
import java.util.stream.*;

class Main {
  static final String ALLOWED_CHARS = "01";
  
  public static void main(String[] args) {
    char[] valid = {'0','0','1','0','0','0','1','1','0','1'};
    char[] invalid = {'0','0','1','0','0','A','1','1','0','1'};
    
    Set<Character> set = ALLOWED_CHARS.chars()
                                      .mapToObj(c -> (char) c)
                                      .collect(Collectors.toSet());

    isValid(set, valid);
    isValid(set, invalid);
  }

  public static void isValid(Set<Character> set, char[] array) {
    for (char c: array) {
      if (!set.contains(c)) {
        System.out.println("Invalid");
        return;
      }
    }
    System.out.println("Valid");
  }
}

Another approach (other than regex) — add the ALLOWED_CHARS to a Set and simple check if any element in the array is not present in the Set.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.