Andrea Manisi
Andrea Manisi

Reputation: 33

Java: Search the first common character between two strings

I'm in trouble to solve this problem. I have to create a method called cercaCarattere that takes in input two strings, compares them and, if it finds a character (the first one, corresponding) must return it, otherwise must return a '*'. Plus, in the main, I must read two strings in a loop, until the character returned by the method and the last character of the first string are different.

This is the code that I wrote

public class prova {

public static char cercaCarattere(String str1, String str2) {
    boolean isCommon = false;
    char letter;
    for(int i=0; i<str1.length() && i<str2.length(); i++) {
        if(str1.charAt(i) == str2.charAt(i)) {
            isCommon = true;
            letter = str1.charAt(i);
        } else {
            isCommon = false;
            letter = '*';
        }
    }

    return letter;
}

public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    String str1, str2;

    System.out.println("Inserire la stringa");
    str1 = stdin.nextLine();
    System.out.println("Inserire la stringa");
    str2 = stdin.nextLine();

    cercaCarattere(str1, str2);
}

In the method, I thought to write a for to read both the strings and check for every character if is the same in both and, if so, to return it, but that return gives me an error because says "letter" is not initialized. How can I solve?

I cannot use Hashset, arrays or other.

Upvotes: 1

Views: 956

Answers (4)

Oleksii Zghurskyi
Oleksii Zghurskyi

Reputation: 4365

Java 8 stream solution:

  • to find matching char at corresponding position
    public static char cercaCarattere(String str1, String str2) {
        return IntStream.range(0, Math.min(str1.length(), str2.length()))
            .filter(i -> str1.charAt(i) == str2.charAt(i))
            .mapToObj(str1::charAt)
            .findFirst()
            .orElse('*');
    }
  • to find first matching char at any position:
    public static char cercaCarattere(String str1, String str2) {
        return str1.chars().mapToObj(c1 -> (char) c1)
            .filter(c1 -> str2.chars().mapToObj(c2 -> (char) c2).anyMatch(c2 -> c1 == c2))
            .findFirst()
            .orElse('*');
    }

Upvotes: 0

Nicholas K
Nicholas K

Reputation: 15423

Make the following two changes :

In your cercaCarattere() once you find the first occurence you can return early. Also the method can be simplified to :

public static char cercaCarattere(String str1, String str2) {
    char letter = '*';
    for (int i = 0; i < str1.length() && i < str2.length(); i++) {
        if (str1.charAt(i) == str2.charAt(i)) {
            return str1.charAt(i);
        } 
    }
    return letter;
}

And, use the value returned by the method to print it out

System.out.println(cercaCarattere(str1, str2));

Upvotes: 2

Ilkhd
Ilkhd

Reputation: 1

They way it is written your code would not the task, even if the compiler did not complain about the variable. The reason compiler spits the error, is because in case both of your strings have zero length, letter indeed stays uninitialized. The second problem is that you should break the loop as soon as you found the match, but the code does not. The right way is this:

char letter = '*';
for(int i=0; i<str1.length() && i<str2.length(); i++) {
    if(str1.charAt(i) == str2.charAt(i)) {
        letter = str1.charAt(i);
        break;
    } 
}

return letter;

Upvotes: -1

Doflamingo19
Doflamingo19

Reputation: 1629

Ciao Andrea, the code is not work because:

  • you must return after letter = str1.charAt(i);
  • you can delete isCommon because you don't use it

So the code becames:

public static char cercaCarattere(String firstString, String secondString) {
    char letter = '*';
    for (int i = 0; i < firstString.length() && i < secondString.length(); i++) {
        if (firstString.charAt(i) == secondString.charAt(i)) {
            return firstString.charAt(i);
        } 
    }
    return letter;
}

Upvotes: 0

Related Questions