0

I want to find a character within a string. If the character is found, add one to char_counter. How can I implement this?

Example:

int char_counter = 0;
String password = "1234";
String search = "1234";
for(int i = 0; i < password.length() - 1; i++) {
    for(int j = 0; j < search.length() - 1; j++) {
        //This is just pseudo code since I don't know how to properly search a string
        if(password[i] == search[j]) {
            char_counter = char_counter + 1;
        }
    }
}
Hamid Shatu
  • 9,503
  • 4
  • 29
  • 40

4 Answers4

2
I don't know what do you want to do if you want to compare your password and search
string character one bye than try this

     int char_counter = 0,i,j;
     String password = "1234";
     String search = "1234";
     if(password.length()==search.length()){    
        for(i=0,j=0;i<password.length();i++,j++){
            if(password.charAt(i)==search.charAt(j)){
                char_counter++;
            }
        }
    }


 if you want to search each character in password string than try this 

  for(int i = 0; i < password.length() - 1; i++){
     for(int j = 0; j < search.length() - 1; j++)
          {
              if(password.length() == search.length())
              { char_counter = char_counter + 1;}
          }
    }
Rishi Dwivedi
  • 908
  • 4
  • 19
1

Your pseudo code:
password[i]

should be:

password.charAt(i)

Result:

if(password.charAt(i) == search.charAt(j)){ 
    char_counter = char_counter + 1;
}
Tyler
  • 16,759
  • 10
  • 49
  • 87
1

Try this..

You should use if you do like this password[i] java will take as array

password.charAt(i) and search.charAt(j)

And you have to use condition as .equals

if(password.charAt(i).equals(search.charAt(i))){ 
    char_counter = char_counter + 1;
}

not password[i] == search[j]

== always just compares two references (for non-primitives, that is) - i.e. it tests whether the two operands refer to the same object.

However, the equals method can be overridden - so two distinct objects can still be equal...... Link

Community
  • 1
  • 1
Hariharan
  • 28,756
  • 7
  • 51
  • 55
  • Just some additional notes, `password[i]` in this case is not valid since `String` is **not** an array of `char` in Java. Instead, Java provides `charAt()` to get the underlying `char` of the `String`. Also, `==` compares values (for primitives), hence `password.charAt(i) == search.charAt(j)` is also valid. – Andrew T. Feb 25 '14 at 04:39
  • Thanks Hariharan!! That was very helpful :) I'm grateful :) – user3160974 Feb 25 '14 at 04:39
  • Thanks Andrew for your additional notes for better understanding :) – user3160974 Feb 25 '14 at 04:40
1

You can first convert you string into an array of characters using toCharArray() method like this:

Char[] pass = password.toCharArray();
Char[] sear = search.toCharArray();

And then use any of comparisons provided by java "equals". "Contains". Of course inside a for(string x: pass). Or a for loop with a counter++

z atef
  • 6,239
  • 3
  • 48
  • 48