In Java, How to Determine If a Char Array Contains a Particular Character

Can you check if an array of char contains a specific char value by converting as list?

You should check what Arrays.asList() returns to you.

It returns you List<char[]> instead of List<Character> because char is primitive. Try using Character[] like the following:

    Character arr[] = {'2', 'a', 'g', '4'};
List<Character> characters = Arrays.asList(arr);
System.out.println("4 in arr : "+ characters.contains('4'));

If statement for checking if a char array contains a user given letter in Java

You don't need to convert your first input to a char[], just leave it as a string and use the contains()

public static void main(String args[]) {
Scanner keyboard = new Scanner(;
System.out.print("Please enter a word: ");
String input = keyboard.nextLine();

System.out.print("Please enter a letter to search in the word: ");
Scanner keyboard1 = new Scanner(;
char letter = keyboard1.nextLine().charAt(0);

// Using toLowerCase() to ignore capital vs lowercase letters.
// Locale may need to be considered.
if (input.toLowerCase().contains(String.valueOf(letter).toLowerCase())) {
System.out.println("The word does contain the letter " + letter + ".");
} else {
System.out.println("The word does not contain the letter " + letter + ".");


enter image description here

enter image description here

Check that string contains a character from array

Streams might not the best fit here. Also, now your solution has quadratic complexity (N*M, where N is the file name length, and M is the size of your illegal character array), which is not very performant. As proposed in the comments, you could use a regular expression:

private static final Pattern ILLEGAL_CHARACTERS_REGEX =

public static boolean fileNameIsValidRegex(String fileName) {
return !ILLEGAL_CHARACTERS_REGEX.matcher(fileName).find();

Or, if your illegal character set is limited to ASCII, you could play around with a bitset to squeeze some performance:

private static final BitSet ILLEGAL_CHARACTERS = new BitSet();

static {
for (char c : new char[]{
'/','\n','\r','\t','\0','\f','`','?','*','\\','<','>','|','\"',':'}) {

public static boolean fileNameIsValid(String fileName) {
return fileName.chars().noneMatch(ILLEGAL_CHARACTERS::get);

Checking if a string contains values from a char array


The above must be


(j can vary only between 0 to 4. So you kept on picking the first four characters from your string)

Also, when did y become a vowel ;)

But you cannot do this within the for loop as you would count a consonant 5 times.

You can have a flag to denote if a vowel was found. If not you increment the consonants count

for (int i=0; i < sentence2.length(); i++) {
boolean vowel = false;
for (int j = 0; j < lookout.length; j++) {
if (sentence2.charAt(i) == lookout[j]) {
vowel = true;
break; //A vowel is found
if (!vowel && Character.isLetter(sentence2.charAt(i))) {

A better way is to use a List or a Set and use contains method as in this answer.

Check char in an char array against a integer

Try this:

if((char)myFile[fileIndex] == '1') {

How to check if certain sequence of characters exists in char array

First, I would make word and arr method arguments. Generating them locally makes your code very difficult to test and debug. Second, you can increment the count when you have reached the end of the word successfully. Something like,

private static int zad4(char[] word, char[] arr) {
int countWord = 0;
for (int p = 0; p < arr.length; p++) {
for (int i = 0; i < word.length && p + i < arr.length; i++) {
if (word[i] != arr[p + i]) {
} else if (i + 1 == word.length) {
return countWord;

Which I tested with

System.out.println(zad4("java".toCharArray(), "javajavajava".toCharArray()));
System.out.println(zad4("java".toCharArray(), "javajava".toCharArray()));
System.out.println(zad4("java".toCharArray(), "java".toCharArray()));
System.out.println(zad4("java".toCharArray(), "ja".toCharArray()));

Getting (as expected)


Comparing characters in 2 char Array using Java

Try this. It will report if either array contains all the characters of the other. I used strings and converted them to arrays to facilitate the coding.

String[][] testData =
{ {"axyzx","xxyzaa"},

for (String[] words : testData) {
boolean result = contains(words[0].toCharArray(), words[1].toCharArray());
String output = String.format("%s contains all %s","'"+words[0]+"'","'"+words[1]+"'");
System.out.printf("%34s - %b%n", output, result);
output = String.format("%s contains all %s","'"+words[1]+"'","'"+words[0]+"'");
result = contains(words[1].toCharArray(), words[0].toCharArray());
System.out.printf("%34s - %b%n%n", output, result);


     'axyzx' contains all 'xxyzaa' - false
'xxyzaa' contains all 'axyzx' - true

'aaxyz' contains all 'aaax' - false
'aaax' contains all 'aaxyz' - false

'axa' contains all 'a' - true
'a' contains all 'axa' - false

'arrs' contains all 'asrrrs' - false
'asrrrs' contains all 'arrs' - true

'acfbe' contains all 'abcdehjfim' - false
'abcdehjfim' contains all 'acfbe' - true

  • Using a map, do a frequency count of the characters in the second array.
  • Now iterate thru the map using the first array, decrementing the character count when the character is found. When the count reaches 0, assign null to the value.
  • if map is now "empty", the first array contains all characters of second array.
// see if first array contains all of second array,
// regardless of order of the characters
public static boolean contains(char[] ch1, char[] ch2) {
// a smaller array cannot possible contain the same
// characters as a larger array
if (ch1.length < ch2.length) {
return false;
Map<Character,Integer> map = new HashMap<>();
// Do a frequency count
for(char c : ch2) {
map.compute(c, (k,v)->v == null ? 1 : v+1);
// now decrement count for each occurrence
// of character in first array, setting value to
// null when count reaches 0.
for(char c : ch1) {
map.computeIfPresent(c, (k,v)-> v <= 1 ? null : v-1);
return map.isEmpty();

Related Topics

Leave a reply