Your code could be simplified a bit:
public static void compare_letters(String name1, String name2) {
int equal_letters = 0 ;
for( char c1 : name1.toCharArray() )
for( char c2 : name2.toCharArray() )
if( c1 == c2 ) equal_letters++;
System.out.println("num of equal letters " + equal_letters);
}
But note that this way of comparing will double-count repeated letters, e.g. comparing "aa"
to "aa"
will return four, because each a
in one string will be compared to each a
in the other.
If you want to count common letters without double-counting repetitions, you'd need to do something like
public static void compare_letters(String name1, String name2) {
// Declare a couple of sets: sets don't allow duplicate elements.
Set<Character> letters1 = new HashSet<Character>();
Set<Character> letters2 = new HashSet<Character>();
// Populate the sets with the letters from the strings.
for( char c : name1.toCharArray() ) letters1.add(c);
for( char c : name2.toCharArray() ) letters2.add(c);
// remove anything that isn't in letters2 from letters1
letters1.retainAll(letters2);
// letters1 is now the set of letters that appear in both names.
// Its size is the number of common letters.
System.out.println("num of equal letters " + letters1.size(););
}