How can I search a string for each character in Java


This question already has an answer here:


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

    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

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 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;}

Your pseudo code: password[i]

should be:



if(password.charAt(i) == search.charAt(j)){ 
    char_counter = char_counter + 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++

Need Your Help

GUIDs as Primary Keys - Offline OLTP

sql-server synchronization guid offline oltp

We are working on designing an application that is typically OLTP (think: purchasing system). However, this one in particular has the need that some users will be offline, so they need to be able to

Why am I having issues using a struct in an abstract class?

c++ struct abstract-class

I've created a simple example (since my classes are rather large). I've managed to reproduce the exact issue with the following code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.