0

I have a text file which contains all five-letter words in Turkish. From there, I want to draw a random word. AssetManager screen printing on all of the word, but somehow I did not drop a single word. How can I use a loop?

I would be glad if you can help.

Source:

public class MainActivity extends Activity {

    Button degistir;
    TextView kelime;
    EditText ykelime;
    Random rnd =new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        degistir=(Button)findViewById(R.id.btngiris);
        kelime=(TextView)findViewById(R.id.kelime);
        ykelime=(EditText)findViewById(R.id.giris);

        AssetManager assetManager = getAssets();

        // To load text file
        InputStream input;
        try {
            input = assetManager.open("5HarfliKelimeler.txt");

            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();

            // byte buffer into a string
            String text = new String(buffer);

            int sayi=rnd.nextInt(text.length());

            kelime.setText(text);
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        degistir.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                kelime.setText(ykelime.getText());
                ykelime.setText(null);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
Damla Umut
  • 3
  • 1
  • 3
  • doe your File exists? is a excpetion thrown? may you could log one, however your should read the input stream until its finished. – MemLeak Aug 08 '14 at 07:26
  • I have the file, you can check all of the words to about 5,000 words, but I want to do a random word from screen to print. – Damla Umut Aug 08 '14 at 07:32

2 Answers2

0

firstly, if your file content won't be changed, then you have to count max line number and write it as constant:

private String getRandomLine() {
        final int FILE_MAX_LINE_INDEX = 5000;//if your file has 5000 lines, and it's content never be changed
        Random rnd = new Random();
        final int lineIndex = rnd.nextInt(FILE_MAX_LINE_INDEX);
        try {
            LineNumberReader lnr = new LineNumberReader(new InputStreamReader(getAssets().open("5HarfliKelimeler.txt")));
            String s;
            while ((s = lnr.readLine()) != null) {
                if (lnr.getLineNumber() == lineIndex) {
                    lnr.close();
                    return s;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

or, if your content may be changed, and you don't know how many lines will be in file:

private String getRandomLine() {
        List<String> fiveLetterWords = new ArrayList<String>(5000);
        try {
            LineNumberReader lnr = new LineNumberReader(new InputStreamReader(getAssets().open("5HarfliKelimeler.txt")));
            String s;
            while ((s = lnr.readLine()) != null) {
                fiveLetterWords.add(s);
            }
            lnr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Random rnd = new Random();
        return fiveLetterWords.get(rnd.nextInt(fiveLetterWords.size()));
    }
Autocrab
  • 3,474
  • 1
  • 15
  • 15
  • Why have to use static max line index ? Add each word to a list and select a random index from it. (from 0 to listSize) @Autocrab – Tugrul Aug 08 '14 at 07:51
  • it will be faster, if you know that content of file won't be changed, so you know, that file contains alsways same count of lines. therefore you can assign this count as constant, and make random int from this value. You won't have to read all lines, you will read only lines before your random index. – Autocrab Aug 08 '14 at 07:54
  • You can easily read 5 k 10k or 50k words in milisecs. Only milisecs... And what if it's 5143 lines ? – Tugrul Aug 08 '14 at 07:58
0

Read file line by line and get all words to a list.(How to read line by line | check here)

Then, specify a random index for each button click. Finally, display the word from the list with generated index. (Index range must be from 0 to size of your list)

-- I edited your code without any editor. --

    public class MainActivity extends Activity {

            Button degistir;

             TextView kelime;

            EditText ykelime;
            List<String> wordList=null;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                degistir=(Button)findViewById(R.id.btngiris);
                kelime=(TextView)findViewById(R.id.kelime);
                ykelime=(EditText)findViewById(R.id.giris);

                AssetManager assetManager = getAssets();

                // To load text file
                InputStream input;
                try {
                    input = assetManager.open("5HarfliKelimeler.txt");
                    wordList= new ArrayList<String>();

       BufferedReader reader = new BufferedReader(new InputStreamReader(input , charset.forName("UTF-8")));

                    String line = "";



                        while ((line = reader.readLine()) != null) {
                           wordList.add(line);
                        }





                } 
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                degistir.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        if(wordList!=null){
                         Random rnd =new Random();
                         Integer generatedIndex = rnd.nextInt(wordList.size());                    
                         String selectedWord=wordList.get(generatedIndex);
                         kelime.setText(selectedWord);
                         // ykelime.setText(null);
                        }
                          else{
                          //DISPLAY MSG ETC.
                         }
                    }

                });
            }
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.main, menu);

                return true;
            }

        }
Community
  • 1
  • 1
Tugrul
  • 1,760
  • 4
  • 24
  • 39
  • Thanks :) Random access before I click the button I just made ​​a little change. You're great :) – Damla Umut Aug 08 '14 at 07:59
  • Well, I entered in EditText words how can I check whether the ArrayList. – Damla Umut Aug 08 '14 at 08:45
  • http://stackoverflow.com/questions/587404/java-finding-objects-in-collections You need to use collection search. Find matches and display them for each character change event. – Tugrul Aug 08 '14 at 08:51
  • Or simple for loop . Not good for performance but easy to implement. – Tugrul Aug 08 '14 at 08:55