Java – Why don’t the characters in StringBuilder change?

Why don’t the characters in StringBuilder change?… here is a solution to the problem.

Why don’t the characters in StringBuilder change?

The character must be entered from the console to change to lowercase letters on this line. But it shows the same word, the symbols do not change.

public class Task {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder(requestString());
        char symbol = requestSymbol().charAt(0);
        int count = 0;

for (int i = 0; i < sb.length(); i++) {
            if (sb.charAt(i) == symbol) {
                sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
                count++;
            }
        }
        System.out.println("Number of entries: " + count);
        System.out.println("Converted string: " + sb);
    }

static String requestString() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter string:");
        return scanner.nextLine();
    }

static String requestSymbol() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the symbol:");
        return scanner.next();
    }
}

Solution

It seems to be a problem with the line :

sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));

It should be:

sb.setCharAt(i, Character.toUpperCase(symbol));

Related Problems and Solutions