Goran Shakeit
Goran Shakeit

Reputation: 5

How to create a loop for

I need to create a loop that adds "o" after each consonant

Upvotes: 0

Views: 101

Answers (2)

ySiggen
ySiggen

Reputation: 503

I am going to walk you through what I corrected and changed in your code to make it work in order to make it quick and easy for you to comprehend why your code doesn't work and why my answer fixes it.

The mistakes you made are basic ones and frankly you shouldn't have to much of a hard time correcting them yourself if you would use a debugger that walks you step by step in how your code works. You should look on how to use a debugger (for example the debugger used in Eclipse, hopefully you are using an IDE to make your life easier).

Firstly, when you are looking for a consonant in your code, you are only walking through the half of it because of your condition for(int x = 0; x<20; x++) since your string holding the consonants if of a length of 40 characters. This means you are missing consonants like the letter s.

Then you are correctly the consonants you find according to your Swedish language game. But you are never handling characters that are not of these found consonants. You should make a case where you handle these "non consonant" letters, may they be vowels or any kind of character (like punctuation marks and so on). I am fixing this with the use of a simple boolean here.

Keep in mind that my goal here is to change your code as little as I can, thus I went for adding a boolean to handle your cases (checking the presence of a consonant). There are, obviously, many other ways to implement what you are trying to do.

Here come the changes you should add to your code:

    /*This comes after your print "På rövarspråk:"*/
    boolean isConsonant = false; //Boolean to check wether there is a consonant or not
    for(int i = 0; i<length; i++) {
        //You didn't go through the whole consonants list you made with your prevision condition
        for(int x = 0; x<consonants.length; x++){
            if(array[i] == consonants[x])
            {
                isConsonant = true; //Set the boolean accordingly
                String add = array[i]+"o"+array[i];
                slang = slang + add;
                break;
            }
        }
        if(!isConsonant){ //If we don't have a consonant, add the char to the result string
            slang += array[i];
        }
        isConsonant = false; //Reset the boolean for the next character
    }
    /*Here you can print the result (slang) if you want, as you did*/

Upvotes: 1

Lord_PedantenStein
Lord_PedantenStein

Reputation: 500

so the idea is to dublicate consonants and put "o" between them, like t becomes tot, s becomes sos. Vocals are just copied. So you need a method that tells you if a given character is a vocal or consonant to base your decision on that.

public static boolean isConsonant(char inputChar){

    final String consonantsx = "bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ"; 
    char consonants[] = consonantsx.toCharArray(); // String to charr

    for(int i=0; i < consonants.length;i++){
        if(inputChar == consonants[i]){ //note that in Strings u use the equals method instead of "=="
            return true;
        }
    }

    return false;
}

Given this method you can use it in the "translator method".

    public String rovarSpraket(String normalString) {

    char[] array = normalString.toCharArray(); // Input to a char array

    System.out.println("På rövarspråk:");

    String slang = "";

    for (int i = 0; i < normalString.length(); i++) {

        String add = "" + array[i];

        if(Goran.isConsonant(array[i])){

            add += "o" + array[i];

        }

        slang += add;

        }

    return slang;
}

This translates stubborn to sostotubobboborornon like in the wikipedia article https://en.wikipedia.org/wiki/R%C3%B6varspr%C3%A5ket.

Upvotes: 0

Related Questions