Click here to Skip to main content
15,885,880 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to incorporate this windows java program into android build but I have not
been able to successfully achieve that. The program reverses inputted string word for word. How do I get the android to reverse sentences just like the code does with the windows

What I have tried:

I have already successfully apllied it in windows and it worked perfectly, but trying to put
infuse it in android has been difficult. Please help me out with it.

Here is my code

Java
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class SubActivity extends Activity 
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main2);
}

//From here is problem. Please help me in fixing it 
public void RevOnClick(View view)
{
EditText main2EditText1 =(EditText)findViewById(R.id.main2EditText1);
String sen = main2EditText1.getText().toString();
String[] senRev = sen.split("\\b");

for (int n = senRev.length - 1; n >= 0; n--) 
{

TextView main2TextView1 = (TextView)findViewById(R.id.main2TextView1);
main2TextView1.setText(senRev[n]);
}
}
}
Posted
Updated 23-Dec-16 16:14pm
v2

1 solution

By putting the setText() inside the for loop, it will always replace existing content with the one from subsequent iteration. That is the mistake. The correct way is to do the reversal of words in the for loop, then display the final reversed words as string in the edit text control only after the end of the for loop. Try this:
C#
String sen = "Welcome to Code Project";

String[] senRev = sen.split("\\b");

StringBuilder builder=new StringBuilder();

for (int n = senRev.length - 1; n >= 0; n--)
{
    builder.append(senRev[n]);
}

TextView main2TextView1 = (TextView)findViewById(R.id.main2TextView1);
main2TextView1.setText(builder.toString());
 
Share this answer
 
v3
Comments
Member 12919641 24-Dec-16 2:28am    
Thanks Peter....all hail

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900