0

I'm learning lambdas and I have an exercise to rewrite the while loop in this code using lambdas. This method gets encoded user input and returns decoded. I faced this problem and can't understand what I'm supposed to do. (I know that it's not hard, but I just can't get the concept.) I didn't find any similar questions here.

p.s. Also, one more qustion - can this while loop( or maybe whole method) be reworked with method reference?

public String decode(String input) {
    StringBuilder letters = new StringBuilder();
    input = input.toUpperCase();
    int i = 0, j = 0;
    while (i < input.length()) {
        char symbol = input.charAt(i);
        char keySymbol = KEY.charAt(j);
        int newIndex = (ALPHABET.indexOf(symbol) - ALPHABET.indexOf(keySymbol)) % ALPHABET.length();
        char newSymbol = ALPHABET.charAt(newIndex);
        letters.append(newSymbol);
        j = ++j % KEY.length();
        i++;
    }
    return letters.toString().toLowerCase();
}
1
  • How do you want the lambda to be? forEeachIndexed(()->{})? Commented Nov 24, 2019 at 5:16

1 Answer 1

1

The code sample below includes the original decode method, and a new decodeLamda method.

The decodeLambda method replaces the iteration over 'input'. Running the sample will show they both have the same output. It is possible to change the loop to a method reference.

public class Main {

public static void main(String[] args) {
    Main main = new Main();
    System.out.println(main.decode("SECRET MESSAGE"));
    System.out.println(main.decodeLambda("SECRET MESSAGE"));
}

public static String KEY = "HOPSCOTCH";
public static String ALPHABET = "ABCDEFGHIJKLMONOPQRSTUVWXYZ";

public String decode(String input) {
    StringBuilder letters = new StringBuilder();
    input = input.toUpperCase();
    int i = 0, j = 0;
    while (i < input.length()) {
        char symbol = input.charAt(i);
        char keySymbol = KEY.charAt(j);
        int newIndex = Math.abs(ALPHABET.indexOf(symbol) - ALPHABET.indexOf(keySymbol)) % ALPHABET.length();
        char newSymbol = ALPHABET.charAt(newIndex);
        letters.append(newSymbol);
        j = ++j % KEY.length();
        i++;
    }
    return letters.toString().toLowerCase();
}

public String decodeLambda(String input) {
    StringBuilder letters = new StringBuilder();
    input = input.toUpperCase();
    var ref = new Object() {
        int j = 0;
    };

    input.chars()
            .forEach( symbol -> {
                char keySymbol = KEY.charAt(ref.j);
                int newIndex = Math.abs(ALPHABET.indexOf(symbol) - ALPHABET.indexOf(keySymbol)) % ALPHABET.length();
                char newSymbol = ALPHABET.charAt(newIndex);
                letters.append(newSymbol);
                ref.j = ++ref.j % KEY.length();
            });

    return letters.toString().toLowerCase();
}

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.