I am developing word scramble android app. I want to display the String of word in Sequence order but in below code it get random word from the dictionary. My question is how to change my code to get words from Dictionary in Sequence Order?
String[] dictionary=
{"One","Server","Terminate","Analyze","Finish","Start","Wonder","Slow"};
r = new Random(System.currentTimeMillis());
newGame();
// shuffle algorithm
private String shuffleWord(String word){
List<String> letters = Arrays.asList(word.split(""));
Collections.shuffle(letters);
String Shuffled="";
for (String letter : letters ){
Shuffled += letter;
}
return Shuffled;
}
private void newGame(){
// get random word from dictionary
currentWord= dictionary[r.nextInt(dictionary.length)];
// show the shuffled word
tv_word.setText(shuffleWord(currentWord));
// clear the textfield
et_guess.setText("");
// switch buttons
b_new.setEnabled(false);
b_check.setEnabled(true);
}
}
Actually you pick your word with a random position
r = new Random(System.currentTimeMillis());
using an integer random from 0 to ArrayOfWords lenght.
currentWord= dictionary[r.nextInt(dictionary.length)];
Why you dont try to make a counter instead?
int r = 0;
while (r<dictionary.length()) {
currentWord = dictionary[r];
r++;
}
Or I don't understand your answer. But there is the pseudo of what I understand.
Related
HI below is the code which gets the four columns data from the curson and put in the 2d array. basically there are two issues one is that i get the last value as nullnullnullnull means all for columns are fetched as null.
the seconds is that i want to print the array in multitextline or if any other widget availabe so that i get four fields in a row. like
id rule_body rule_con boole
0 abc def 1
0 a f 0
c.moveToFirst();
int i=0;
while(c.moveToNext()) {
String id = c.getString(c.getColumnIndex("id"));
String rb = c.getString(c.getColumnIndex("rule_body"));
String cn = c.getString(c.getColumnIndex("rule_cons"));
String bl = c.getString(c.getColumnIndex("boole"));
table[i][0] = id;
table[i][1] = rb;
table[i][2] = cn;
table[i][3] = bl;
++i;
}
for(int a=0;a<count_row;a++)
for(int b=0;b<count_col;b++) {
obj_ml.append(String.valueOf(table[a][b]));
}
so far i am getting all the result in a single line. any help will be appreciated.
Change your for-loop as below
for (int a=0;a<count_row;a++)
{
for(int b=0;b<count_col;b++)
{
obj_ml.append(String.valueOf(table[a][b]));
}
// add to obj_ml new line character '\n'
obj_ml.append("\n");
}
I want to make a random array and print it over one by one. But I need to print all of it without make any duplicate. I've try to adding it into list but it seems fail.
My Code :
String quest1 = "5x5#5*10#8/4"
String[] quest = quest1.split("#");
ArrayList <String> question = new ArrayList<String>();
question.add(quest[0]);
question.add(quest[1]);
question.add(quest[2]);
Random rand = new Random();
int id = rand.nextInt(question.size());
System.out.println(question.get(id));
question.remove(id);
I want to print 5x5 5*10 8/4 but in random order and I want to print each of it without print it again.
Make a key value 2d object array or a hashmap of integer and boolean. Against all the numbers keep the boolean value false (implying that the number hasn't been printed yet ). Then generate a random number using Random class. Let this be n. Calculate n%array.length. Now see if for this new index whether you have true/false in the 2dArray/hashmap. If false then print the corresponding number and else don't print anything. I hope it's clear to you
Try following code
ArrayList<Integer> questionPrinted = new ArrayList<Integer>();
int i=0;
while (question.size()>0) {
Random rand = new Random();
int id = rand.nextInt(question.size());
if (questionPrinted.size() > 0) {
if (questionPrinted.contains(id)) {
while (!questionPrinted.contains(id))
id = rand.nextInt(question.size());
}
}
questionPrinted.add(i);
System.out.println(question.get(id));
question.remove(id);
i++;
}
I am using a database to show random message at the push of a button.
Every time the button is pushed, a random number is generated and displays a message that corresponds to that number.
Of course, the same message can appear twice as the same number can be generated twice.
So I am creating a string, and I am concatenating each number when the button is pushed.
If the new random number is in the String, then I want to get another number. If that number is in that String, I want to get another number etc etc (Regression?).
I also want to have a global count that I can change, so if the String reaches a size of 9 numbers, it will be reset to "".
String randomList; //global
final int MAX_STRING_LENGTH = 9;
Integer randomNumber = getRandomMessage(messages.size());
if(randomList.length() > 0)
{
if(!randoms.contains(randomNumber.toString()))
{
messageText.setText(messages.get(randomNumber));
}
}
Create a boolean Array of 9 elements
boolean[] check = new boolean[9];
Everytime you use a number, set check[number] to true
void useNumber(int number){
check[number] = true;
}
To check if you already used this number
boolean checkNumber(int number){
return check[number];
}
To reset check
void resetCheck(){
for(int i = 0; i < check.length; i++) check[i] = false;
}
Edit:
Get a string for a given number, resetCheck if necessary;
String getString(int number){
if(usedString == check.length){
resetCheck();
usedString = 0;
}
if(checkNumber(number)){
return getString(getRandomNumber());
}else{
usedString++;
return strings[number];
}
}
I think you can better use a boolean array. You can store the boolean array with the SharedPreferences class. A boolean array is more flexible and you can retrieve the values in constant time. You can also easily make it larger when you have more random messages.
Do a simple trick Hope this works for you
buttonRandom.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Collections.shuffle(messages);
messageText.setText(messages.get(0));
}
});
I want to compare two sets of digits. The first I will generate as a random in code. The second will be entered into an EditText.
I wish to compare the two numbers and see if they match.
Random number:
public static int random() {
if (randomNumberGenerator == null)
initRNG();
return randomNumberGenerator.nextInt();
}
private static Random randomNumberGenerator;
private static synchronized void initRNG() {
if (randomNumberGenerator == null)
randomNumberGenerator = new Random();
}
Showing randomly selected number
display = (TextView) findViewById (R.id.textView1);
display.setText ("Random Number:" + random ());
How can I compare the two numbers?
You can use something like the following:
display = (TextView) findViewById (R.id.textView1);
EditText myEditText = ...; // Find your EditText, maybe by ID?
int random = random(); // Get your random value
display.setText("Random Number:" + random()); // Show the random number
int numberEntered = -1; // Default value for numberEntered; should be a value you wouldn't get randomly
try {
// Here, we try to make a number out of the EditText; if it fails, we get a Toast error
numberEntered = Integer.parseInt(myEditText.getText().toString());
} catch (NumberFormatException nfe) {
Toast.makeText(myEditText.getContext(), "That's not a number!", Toast.LENGTH_LONG).show(); // Tell the user they didn't enter a number
}
if (random == numberEntered) {
// HAPPY DAY, THEY MATCH!
} else {
// No luck :(
}
Just make a subtraction between both numbers and if the result is 0, both numbers match.
EDIT: Subtraction was just a way to show that both were numbers. Obviously it makes more sense to compare both numbers directly rather than subtracting and comparing then.
Using the text to speech API I want to change the string array to increment from index 0 string and when it reaches the end it will go back to the beginning.
At the moment it uses a random generator and the method works as follows:
public static void sayHello() {
// Select a random hello.
int helloLength = HELLOS.length;
String hello = HELLOS[RANDOM.nextInt(helloLength)];
mTts.speak(hello,
TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue.
null);
}
HELLOS is the array and it holds the strings:
String [] HELLOS = {"One" "Two" "Three" "Four"};
Appreciate any help
Thanks.
When you want to increment an index but loop around to zero again modulo is your friend.
int currentHelloIndex = 0;
public static void sayHello() {
// Select a random hello.
int helloLength = HELLOS.length;
String hello = HELLOS[currentHelloIndex];
currentHelloIndex = (currentHelloIndex + 1) % helloLength;
mTts.speak(hello,
TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue.
null);
}