Sign in to follow this  
GameDeveloper93

Java how to do this

Recommended Posts

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?

Share this post


Link to post
Share on other sites
DiegoSLTS    2113

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[i] with petLang[i]. 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.

Edited by DiegoSLTS

Share this post


Link to post
Share on other sites
Glass_Knife    8636

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 );

Share this post


Link to post
Share on other sites
alnite    3438

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;
Edited by alnite

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this