• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps mixing letters in a sentence

Hello,

I have a problem with a current project.It's a simple one,i know,but i'm not sure how to solve it.I need to make a application that shuffles a sentence something like this:
before:I am going to school today.
after:I am giong to shcool tdaoy.

I need to change only the middle of each word and keep the fist and last letter intact.
This is what i've done so far:
package alex.com;
import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MixWords extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText TextEdit = (EditText) findViewById(R.id.TextEdit);
final TextView mix =(TextView) findViewById(R.id.mix);
final Button button = (Button) findViewById(R.id.proces);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {


// Perform action when clicking
MixMe(TextEdit.getText().toString());

}

private void MixMe(String word) {
// TODO Auto-generated method stub
ArrayList<Character> all = new ArrayList<Character>();


for (int i = 1; i < TextEdit.length()-1; i++) {//starts from the second character
all.add(word.charAt(i));
}
Collections.shuffle(all);

char a = word.charAt(0);//The first char

char b = word.charAt(TextEdit.length()-1);//The last char

String result = "";
result+=a;


for (Character character : all) {
result += character;
}
result+=b;


mix.setText(result);//shows the the mix string


}
});
}
}


I need to search for whitespaces and somehow split the string in order to jumble only the middle of the string.Any ideas?Thank you
 
I should have fixed it in a way like this, maybe it helps you out because I don't understand your question.

Code:
private String shuffleThings(String a) {
                    StringBuffer buf = new StringBuffer();
                    String[] words = a.split(" ");
                    int wordLength;
                    for (String word : words) {
                        wordLength = word.length();
                        if (wordLength <= 2) {
                            buf.append(word);
                        } else {
                            buf.append(word.charAt(0));
                            buf.append(word.substring(1, wordLength-1));
                            buf.append(word.charAt(wordLength-1));
                        }
                        buf.append(' ');
                    }
                    return buf.toString().trim();
                }
                
                private String shuffleString(String toShuffle) {
                    int strLength = toShuffle.length();
                    if (strLength <= 1) {
                        return toShuffle;
                    }
                    int pivot = strLength / 2;
                    String part1 = shuffleString(toShuffle.substring(0, pivot));
                    String part2 = shuffleString(toShuffle.substring(pivot));
                    if (Math.random() > 0.5) {
                        return part1 + part2;
                    } else {
                        return part2 + part1;
                    }
                }
 
Ok.Thank you.You are great!.My question was how can i detect spaces beetween the words,but i think you solved it.Now the problem is,if I want ot print the final result on the TexView(sorry,i'm kind of new with Android)..
someting like :
mix.setText(result),or mix.setText(part1+part2).
Thanks again very much!.:)
 
Well, I assume there is some input field, like a textfield. You get the value of the textfield using the following code:
Code:
TextView inputTextView = (TextView) findViewById(R.id.yourTextViewId);
String inputString = inputTextView.getText();
Then you need to do the randomnizing and placing it back in someting like a lable or TextView as you suggested.
Code:
String outputString = ShuffleThings(inputString);
TextView outputTextView = (TextView) findViewById(R.id.yourOutputTextViewId);
outputTextView.setText(outputString);

It might be useful for you to check out some Android Sample code:
Technical Resources | Android Developers
 
using JordiDroid's code , I think you'll end up with an array of Strings that are done with the shuffling
so then you'll need to connect 'em together
Code:
String sentance = "";
for(int i = 0;i<words.length();i++)
sentance = sentance + words[i] + " ";
sentance = sentance.trim();
then you'll need to put it in a textview using setText
 
Thanks guys.And if i want to use split string on my code ?Can it work?I'm thinking of spliting the string into each word separtly,and to shuffle each word with a for statment.The problem is the whitespaces beetween the words.Please give an example(if it's posibile on my code)

Thank you very much.You're life savers.


package alex.com;
import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MixWords extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText TextEdit = (EditText) findViewById(R.id.TextEdit);
final TextView mix =(TextView) findViewById(R.id.mix);
final Button button = (Button) findViewById(R.id.proces);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {


// Perform action when clicking
MixMe(TextEdit.getText().toString());

}

private void MixMe(String word) {
// TODO Auto-generated method stub
ArrayList<Character> all = new ArrayList<Character>();



for (int i = 1; i < TextEdit.length()-1; i++) {//starts from the second character
all.add(word.charAt(i));
}
Collections.shuffle(all);

char a = word.charAt(0);//The first char

char b = word.charAt(TextEdit.length()-1);//The last char

String result = "";
result+=a;


for (Character character : all) {
result += character;
}
result+=b;


mix.setText(result);//shows the the mix string


}
});
}
}
 
I need to split the enterd sentence into words and after that assign each word to a variable,in order to suffle each word at the time,and after that printing them all together in a setText(with whitespaces),but i'm not sure how to do that.Maybe with tokenize?Please Help!
 
to split a sentance you can use JodiDroid's code
like
Code:
String [] splittedSentance = sentance.split(" ");
then just iterate over the array string normally
if you want to use tokenizer, it's gonna be something like this
Code:
StringTokenizer tok = new StringTokenizer(sentance);
then to iterate over it , you can use this code
Code:
String temp;
while(tok.hasNext()){
temp = tok.nextToken();
//Do something with temp here
}

also when posting code , please use the "
Code:
" tag and end it with a "[/ CODE]" without spaces and quotes
 
Ok.I got the part with the split,but what about the shuffle part?Should I do it with Collections.shuffle,like in my example,or is there a better way?Thnk guys and Happy Easter!:)
 
well .. if you found a way that works then use it
there could be a better way to do stuff .. but as long as it's working properly then it's fine
you can research later on to optimize stuff .. now get something working then start optimizing :D
 
Yes,but....I don't think it will work if I'm using my way,and I'm looking for a different way,one that works with split.string.
Shuffle.collection worked onn my example,but i'm not sure it will work on the new code.thank you for all your help!
 
Back
Top Bottom