A friend and I have an inside joke; well, not exactly inside if it is based off an existing meme. We would send messages in the 'retarded' form, as the meme 'Retarded Spongebob' does in the example "i WoUlD aPPreCiaTE sOmE fEeDbAcK oN mY CoDE". It takes in an input, loops through the character array formed from the string input, randomises the capitalisation, and the entire array is put back for output. Any improvements that can be suggested?

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class RetardedSpongebobifier {
    private char prevChar = 'z';

    public static void main(String[] args) {

        // Take input of sentence
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter desired sentence to RS.");
        // Save input
        String input = scanner.nextLine();
        // Turn input into a character array
        char[] inputArray = input.toCharArray();

        RetardedSpongebobifier obj = new RetardedSpongebobifier();
        // Create array to hold new array
        char[] charArray = new char[input.length()];

        // Run through the list of characters, maybe changing them to uppercase or lowercase
        for (int i = 0; i < input.length(); i++) {
            // Send character for random capitalizing
            char adding = obj.randCap(inputArray[i]);
            // Add uppercase/lowercase character back into the character array
            charArray[i] = adding;

        // Print array
        System.out.print(new String(charArray));

    private char randCap(char character) {

        // Check if character is not any of the blacklisted
        if (character == 'i' || character == 'l' || character == ' ' || character == ',' || character == '.') {
            prevChar = character;
            return character;
        else {
            // Random TRUE/FALSE
            boolean isCap = (Math.random() * 100) >= 50;
            if (isCap && Character.isLowerCase(prevChar)) {
                prevChar = character;
                // Return uppercase letter
                return Character.toUpperCase(character);
            else {
                prevChar = character;
                // Return lowercase letter
                return Character.toLowerCase(character);
  • 2
    \$\begingroup\$ Haha, +1 for the classname :) \$\endgroup\$ Commented Sep 19, 2017 at 13:43

2 Answers 2


Unnecessary comments

You have a lot of comments telling WHAT the code does. Comments should tell -why- you do it in a certain way or what else you have tried but failed. Just repeating in comment what the code says is a waste of time (for the reader and the writer as well)

Method name

You method is called randCap. I prefer just naming it what it does, so randomCapitalize() for example. Makes the code more readable.


I tried to understand what the logic behind the prevCharacter is. It is not immediately clear how it works, and what is has to do with the blacklist.

I think you mean to make sure there are not many lowercase or uppercase characters in a row?

You don't need such a long blacklist, because you can safely call toUpperCase() and toLowerCase() on comma's etc.

Code in main loop

You can move a lot of the code out of the main loop, by adding a method public String spongeBobify(String s) to the class. The main method will only take care of the input/ouput.

Unnecessary calculations

What is wrong with:

boolean isCap = Math.random() >= 0.5;

or, if you have a Random object, random.nextBoolean()?

Closing the scanner

You need to close your Scanner, you can do this by using a try-with-resources:

try (Scanner scanner = new Scanner(System.in)) {
  • \$\begingroup\$ Brilliant editing, I didn't think of the random math part. As to prevCharacter, yes indeed, the idea is to avoid having two capitalized characters together, though it does not seem to work. Any ideas? \$\endgroup\$ Commented Sep 20, 2017 at 4:57
  • 1
    \$\begingroup\$ The hint on how to do it is in your comment. You need to check if the past two characters where capitals. Just count each added (if a uppercase) letters using int capitalCount. If it was a lowercare reset the counter to 0. Increase it if the last letter was indeed a capital. If the count == 2, add a lowercase letter. \$\endgroup\$ Commented Sep 20, 2017 at 6:49

The prevChar thing isn't working since the uppercase letter isn't set to prevChar, it's just returned.

if (isCap && Character.isLowerCase(prevChar)) {
    prevChar = Character.toUpperCase(character);
    // Return uppercase letter
    return prevChar;

Hope this does the trick :)


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.