I'm trying to make a simple game where the sentence will be shuffled but I want to split it with each word. How can I loop my string value word by word just like in this Sample Image.
public class MainActivity extends AppCompatActivity {
TextView textview1, textview2;
CardView text1;
//Initializing string variable.
String value = "Testing to shuffle an array" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview1 = findViewById(R.id.textView1);
textview2 = findViewById(R.id.textView2);
text1= findViewById(R.id.txt1);
//Converting string variable to byte array.
String[] words=value.split(" ");
//printing string on screen.
textview1.setText("String = " + value);
//Printing array on screen.
textview2.setText("Shuffled = ");
Random r = new Random();
for (int i = words.length-1; i >= 0; i--) {
// Pick a random index from 0 to i
int j = r.nextInt(i+1);
// Swap words[i] with the element at random index
String temp = words[i];
words[i] = words[j];
words[j] = temp;
textview2.setText(textview2.getText() +""+ words[i]+" ");
}
}
}