0

I just began developing android apps and i suddenly found it difficult reading a word from a text file, randomly,when a button is clicked.i tried putting all the words from the text file into an array and told it to display the word until the next line( randomly), but it doesn't seem to work. I want to know how to read and display words from a text file one by one and Randomly, every time a button is clicked !!!

  • Can you share your current code? – Tom Sep 05 '14 at 10:09
  • Read this about how to [read a text file in Android](http://stackoverflow.com/questions/12421814/how-can-i-read-a-text-file-in-android). I don't think you can read "randomly" a text file since a cursor needs at least to be set before reading anything. Best way to achieve what you want imho would be to read the entire text file, store words in a collection then randomize the word selection within this collection. – m4rtin Sep 05 '14 at 10:10
  • You had store words of textfile into array, so something is wrong in 'word select' and/or 'display word'. How about to develop a program which display a word randomly from array of words? – Fumu 7 Sep 05 '14 at 10:23

4 Answers4

0

You should try splitting the text word by word and putting it in an array.

m4rtin
  • 2,445
  • 22
  • 34
York Xtrem
  • 58
  • 1
  • 10
0

Create one big string by reading the file to the end. Split the big string into an arraylist by using string.split and give the split method the correct split parameter (" " or ","). Take a random entry out of the arraylist, where the maximum allowed random is the size of the arralist.

Mr Jones
  • 134
  • 7
0

Sample code for displaying a word from an array of words.

Functions of most lines are written in comment.

string words[]; // array for words.
   /* code for reading text from text file and place words in text into words[] */
int sizeOfArray = numberOfWordsInArray; /* the value is set in the code of words placing into array */

Random rnd = new Random(); // random number generator

int index = rnd.nextInt(sizeOfArray); // nextInt returns random integer number between 0 and (sizeOfArray-1).

printf("%s\n",words[index]); // select a word by random number and display it.
Fumu 7
  • 1,091
  • 1
  • 7
  • 8
0

I guess you have one word per line. Use code below to read file and save it into list.

    ArrayList<String> list = new ArrayList<String>();
try {
    InputStream instream = openFileInput("yourfile.txt");
    if (instream) {
        BufferedReader buffreader = new BufferedReader(new InputStreamReader(instream));
        String line;
        while (( line = buffreader.readLine())) {
        list.add(line);
    }
}
instream.close();
} catch (java.io.FileNotFoundException e) {
} 

Place above code inside onCreate method. Now use import java.util.Random to randomly select your item from list. Place below code inside OnClickListener.

public Item anyItem() 
    {   private Random randomGenerator = new Random();
        int index = randomGenerator.nextInt(list.size());
        Item item = list.get(index);
        System.out.println("Your Selected item is " + item");
        return item;
    }

Now you can do anything with the item returned from above anyItem() method, hoped it helped you.

Crawler
  • 1,988
  • 3
  • 21
  • 42