简体   繁体   中英

Can't seem to clone char in a string

I have this assignment in school to check input string (through BufferedReader), if that string contains any vowels (like, in my language, a, ā, e, ē etc) and if they are there, you have to put character 'p' after that vowel and even after that 'p' char you have to put that vowel, after which that 'p' was inserted.

Sounds like this: dog => dopog; snails => snapaipils and so on. The thing is - I made it working so that this char 'p' is in the right spot, but I can't put that vowel after char 'p'.

For now I am getting like, dog => dopg.

In my code here:

 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Ld2151rdb255 { static char[] patskani = { 'a', 'ā', 'e', 'ē', 'i', 'ī', 'u', 'ū', 'o' }; static char character; static StringBuilder input; public static void main(String[] args) { char charAtPosition; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { String valueEntered = br.readLine(); valueEntered = valueEntered.toLowerCase(); input = new StringBuilder(valueEntered); // should use "String.contain" for (int i = 0; i < input.length(); i++) { for (int j = 0; j < patskani.length; j++) { charAtPosition = input.charAt(i); if (charAtPosition == patskani[j]) { input.insert(i + 1, 'p'); // input.insert(i + 2, charAtPosition); } } } } catch (IOException e) { System.out.println("input-output error"); } // Rezultātu izvade String result = input.toString(); System.out.println(result); } } 

I tried to put input.insert(i + 2, charAtPosition);, but yea - it smells like IndexOutOfBoundary exception. It really is, no matter, how I changed the index and what method I approached to do the task.

Don't worry much about object 'patskani'. Just a 'vowels' in my language.

Any help would be appreciated, because I am kinda stuck there and don't know if there's a way of doing it just by continuing or I have to rewrite the logic.

Thanks in advance!

I would suggest that you build the output step by step, that will be easier. So get the char, add it to the output, check if it's a patskani and if so append a p :

input = new StringBuilder(); // should be named output most likely    

for (int i = 0; i < valueEntered.length(); i++) {
    // get the i-th char
    charAtPosition = valueEntered.charAt(i);
    // append this char to "output"
    input.append(charAtPosition);
    // check if the char is in "patskani"
    for (int j = 0; j < patskani.length; j++) {
        if (charAtPosition == patskani[j]) {
            // if so, append a "p" and no need to continue checking
            input.append('p');
            break;
        }
    }
}

Something like that (demo here ). As a side note, note that there is something called enhanced for loop that can be handy.

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