Click here to Skip to main content
13,258,269 members (49,348 online)
Rate this:
 
Please Sign up or sign in to vote.
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

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 23-Dec-16 9:23am
Updated 23-Dec-16 17:14pm
Maciej Los257.6K
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
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());
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.171114.1 | Last Updated 23 Dec 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100