Guessing game for a five-digit code
Streams clean this up a little bit, however I removed print statements here so that the logic can be seen a little clearer:

String code = "53492";
int guesses = 10;
Scanner scanner = new Scanner(System.in);

while (guesses-- &gt; 0) {
    String guess = scanner.nextLine();
    if (guess.equals(code)) {
        return; //guess was correct, can also just compare correct to guess length
    }

    int correct = compare(guess, code, Stream::count); //count elems
    int combo = compare(guess, code, s -&gt; s.reduce(0, Integer::sum)); //sum
    // print information, note this requires three iterations since we stream twice
    // and have an #equals check. There are still some optimizations you
    // can make to remove this
}

//elsewheres
public &lt;R&gt; R compare(String one, String two, Function&lt;IntStream, R&gt; back) {
    //apply our stream function to an intstream for a mutable return
    return back.apply(IntStream.range(0, code.length())
                      .filter(i -&gt; guess.charAt(i) == code.charAt(i)) //correct chars
                      .map(i -&gt; guess.charAt(i) - '0')); //map to values
}


It seemed like it would be a neater solution before I actually sat and wrote it, but I figured it'd be nice to at least see an alternative.