Java how to do this

Started by
2 comments, last by alnite 9 years, 2 months ago

If you look at the below code


     for(int i =0; i<enLang.length; i++) {
                 strOutput = english.getText().toString().replaceAll(enLang[i], petLang[i]);
         }

id like to take all values from the array enLang in order and replace them with the values from petLang

in PHP i would just do this


preg_replace($words['enLang'],$words['petLang'],$str);

whats the equivalent of the above PHP?

Advertisement

The "replaceAll" method on the string class in Java affects the string on which you called the method. It searches every match of the first argument (used as a regular expression) and replaces them with the second parameter. Look at the documentation here: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29

I suppose enLang is an array of strings. In that case you can probably do something like:


for(int i =0; i<enLang.length; i++) {
    enLang[i] = petLang[i];
}

And now enLang has all the values replaced by petLang. That's what you want?

EDIT: OK, after reading what preg_replace does I think I understood what you want to do. There's no function that does exactly that, and it's harder than it looks in Java.

You have some text that you want to translate, and you want to search every ocurrence of each english word and replace that word with it's counterpart on another language.

The first thing you have to consider is that strings in Java are immutable, so replaceAll returns a new string using what "english.getText().toString()" and replacing every ocurrence of enLan with petLang. This means that if you always use "english.getText().toString()" on each iteration you're using the english text, not the modified text of the previous iteration.

So, first, you need to do something like this:


string strOutput = english.getText().toString();
for(int i =0; i<enLang.length; i++) {
    strOutput = strOutput.replaceAll(enLang[i], petLang[i]);
}

And now if "english.getText().toString()" returns "hello world", strOutput will start with "hello world" as the value, then "<hello in petLang> world" and later "<hello in petLang> <world in petLang>".

But, this has one issue. I guess preg_replace in php does only one run over the string, so a word that was replaced can't be replaced by another word. I don't know what "pet" language is, so I'll give an example on an imaginary language.

Imagine again the "hello world" string, and imagine that in the imaginary language "hello" is translated as "world", and "world" is translated as "hello". The first iteration on the string will search for all ocurrences of "hello" and replace them with "world", so strOutput will have the value of "world world". Now on the next iteration all ocurrences of "world" will be replaced by "hello", so your output is "hello hello" in the end instead of "world hello".

So, the proper way to do it would be to split the string in an array of words (http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-), translate each word and then join every word in the same order. There's no equivalent for PHP's "join", but it's easy to implement.

We need some more code here. And it would help future readers if you describe what the outcome is.

what is the english.getText() class?

Maybe something like:


strOutput = english.getText().toString().replaceAll(enLang[i], petLang[i]);
english.setText( strOutput );

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

I think the problem is that you are not saving the resulting string from the previous invocations, so strOutput only yields the result from the last replaceAll().


strOutput = english.getText().toString()
for (int i = 0; i < enLang.length; i++) {
    strOutput = strOutput.replaceAll(enLang[i], petLang[i]);
}
return strOutput;

This topic is closed to new replies.

Advertisement