简体   繁体   中英

Questions regarding String.charAt(); in Java

I am recently working on an algorithm question from Leetcode. But I am really confused with this line of code shown below. And I do not actually understand what is the meaning of

(char)(str.charAt(loop) + 1))

Can anyone explain this to me? Furthermore, sb means

StringBuilder sb = new StringBuilder();

In case you guys do not know what is sb .

Here is the full code:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.nextLine();
            StringBuilder sb = new StringBuilder();
            for (int loop = 0; loop < str.length(); loop++) {
                if (str.charAt(loop) == 'Z') {
                    sb.append("a");
                    continue;
                }
                if (str.charAt(loop) >= '0' && str.charAt(loop) <= '9') {
                    sb.append(String.valueOf(str.charAt(loop)));
                    continue;
                }
                if (str.charAt(loop) >= 'A' && str.charAt(loop) < 'Z') {
                    sb.append(String.valueOf((char)(str.charAt(loop) + 1)).toLowerCase());
                    continue;
                }
                else {
                    switch (str.charAt(loop)) {
                        case('1'):
                            sb.append("1");
                            break;

                        case('a'):
                        case('b'):
                        case('c'):
                            sb.append("2");
                            break;

                        case('d'):
                        case('e'):
                        case('f'):
                            sb.append("3");
                            break;

                        case('g'):
                        case('h'):
                        case('i'):
                            sb.append("4");
                            break;

                        case('j'):
                        case('k'):
                        case('l'):
                            sb.append("5");
                            break;

                        case('m'):
                        case('n'):
                        case('o'):
                            sb.append("6");
                            break;

                        case('p'):
                        case('q'):
                        case('r'):
                        case('s'):
                            sb.append("7");
                            break;

                        case('t'):
                        case('u'):
                        case('v'):
                            sb.append("8");
                            break;

                        case('w'):
                        case('x'):
                        case('y'):
                        case('z'):
                            sb.append("9");
                            break;

                    }
                }
            }
            System.out.println(sb.toString());
        }
    }
}

You have to dissect the expression into its parts:

String.valueOf(
    (char)
      (str.charAt(loop) + 1)
).toLowerCase());

So, on the "innermost" call (str.charAt(loop) + 1) a single char value is retrieved from str . Then 1 is added to that (remember that char is also a "numerical" type that allows for numerical operations). Then the result of that is turned into a String again, when is then forced to be a lower case string.

For the why to do that we would need more context.

This line of code is simply encoding a string by caesar ciper of +1, in other word, if you put this line in a loop it will create a string of every char is the upper char in the alphabet, for example banana will be cboboc For more about caesar ciper look in https://en.m.wikipedia.org/wiki/Caesar_cipher

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM