0

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

2
  • Hint: what does 20 represent? Commented Nov 21, 2016 at 20:58
  • He wants to go through his consonants list, but he didn't take in account there are lowercase and upercase letters in his string! Commented Nov 21, 2016 at 21:19

2 Answers 2

1

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*/
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Based on my earlier code and from the help in the other question this one really worked perfect. Just one thing I thought about " if(inputChar == consonants[i]){ //note that in Strings u use the equals method instead of "==" " You used == in the code but you said use equals instead?
Yes if a String would be compared you would use equals (like String1.equals(String2)). In the example Chars are compared so "Char1 == Char2" is used. I just wanted to point that out because it is a common thing to go wrong.

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.