Kaleb D
Kaleb D

Reputation: 3

How can I check if a character in a string is equal to any position in an array?

I'm trying to compare the first character of a string to make sure it isn't equal to any position in an array. Here's the code:

if(string.charAt(0) != array[0] || string.charAt(0) != array[1])
{
    //Code here
}

I check an array with 10 positions in the actual code, but this is just too much to type. Is there a simpler way to do this? If so, how?

Upvotes: 0

Views: 1930

Answers (7)

progrenhard
progrenhard

Reputation: 2363

Use a for loop

for(int i = 0; i < array.length; i++){
    if(string.charAt(0) != array[i])
    {
       //Code here
       break;
    }  
}

Or maybe even a while loop

int i = 0;
while(i < array.length){
    if(string.charAt(0) != array[i])
    {
       //Code here
       break;
    }  
}

Why the hell not lets screw iteration and do some recursion. (assuming array's type is char). The other methods are better I just wanted to show that you can do it with recursion.

boolean checkChar(String string Char[] array){
    return checkCharHelper(string, array, 0);
}

boolean checkCharHelper(String string, Char[] array, int index){
   if(index >= string.length){
      return false;
   }
   if(string.charAt(0) != array[i])
   {
      //Code here
     return true;
   }
   return checkCharHelper(string, array, index + 1);  
}

Upvotes: 2

bohlmanc
bohlmanc

Reputation: 1

I think a for loop is the best option. Loop through the array, checking each index for the character at the beginning of your string.

//Your string
String yourString = "some string";

boolean interrupt = false;

for (int i = 0; i < array.length; i++) {
     if(yourString.charAt(0).equalsIgnoreCase(array[i])) {
          interrupt = true;
          break;
     }
}
if(interrupt) {
    System.out.println("Found in array");
}
else {
    System.out.println("Not found in array");

Upvotes: 0

gkrls
gkrls

Reputation: 2664

Create a seperate method for it iterating through the array and comparing each char in the array with the give char, in this case the first char of a string.

 private boolean charInArray(char[] chars, char c){
        for(char x: chars)
           if(c == x) return true;

        return false;
 }

You can call it like that:

if(!charInArray(array, string.charAt(0)){
    //do something
}

Upvotes: 0

kajacx
kajacx

Reputation: 12939

Java 8 powa

int[] array = {'a','b','c'};
String s1 = "cat";
String s2 = "dog";

boolean res1 = Arrays.stream(array).anyMatch(c -> c==s1.charAt(0));
boolean res2 = Arrays.stream(array).anyMatch(c -> c==s2.charAt(0));

System.out.println(res1); //true
System.out.println(res2); //false

Upvotes: 1

merlin2011
merlin2011

Reputation: 75545

Loop through the array and check each position. Set a flag if you find one that matches.

boolean flag = false;
for (int i = 0; i < array.length; i++) {
    if (string.charAt(0) == array[i]) {
        flag = true;
        break;
    }
}

if (flag) {
    // Indicates that one of them was equal. 
} else {
    // None of them was equal.
}

Upvotes: 0

Braj
Braj

Reputation: 46841

Use List instead of static array and then use List#contains() method as well.

sample code:

List<Character> list = Arrays.asList('a', 'b', 'c');
if (!list.contains(string.charAt(0))) {
    System.out.println("not found in array");
}

Upvotes: 0

wvdz
wvdz

Reputation: 16641

for (char c : array) {
    if (string.charAt(0) != c) {
         // Code here
         break;
    }
}

Upvotes: 0

Related Questions