How to get a random line from text file in Android using Android Studio 2.1.3?
Asked Answered
M

1

2

I have a text file with 500 lines. I placed this text file in app/src/main/assets folder with the name "words.txt". In this file each line is separated with line break. Now i need to get random line from this text file. I visited following questions prior to posting this.

How to load random line from text file in android?

InputStreamReader and reading random lines from .txt file

How to grab a random line from a text file and print the line [duplicate]

How to get a random line of a text file in Java?

Reading a random line from text file in android

I have constructed my code from above links as well as this one. I did not know that some class called line number reader exists.

Here is my code:

try {
        //Initialize assetmanager class
        AssetManager am = this.getAssets();
        //open file using asset manager
        InputStream is = am.open("words.txt");
        //read buffer manager
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        //Important: use of LineNumberReader Class
        LineNumberReader lnr = new LineNumberReader(reader);
        Random r = new Random();
        int n = r.nextInt(500)+1;
        lnr.setLineNumber(n);
        mWord = lnr.readLine();
        Log.d("MyLog","The letter is "+mWord);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Now this code works but always gives the first line. The random number is generated but the lnr (LineNumberReader) reads first line always. Why?

This is for learning purpose. For current case i know the length of file (or total no of lines) are 500.

Mellicent answered 19/8, 2016 at 7:11 Comment(7)
Your readLine() reads the first line as it starts at the beginning of the file. If you would call readLine() ten times then the tenth time it would read the tenth line.Just
LineNumberReader ??? Never saw that. Where does it come from? Strange name also. May first comment is wrong it looks now. What are the values of n that you generate?Just
If it does not work then just use a normal readLine() and call it n times in a loop.Just
@Just docs.oracle.com/javase/7/docs/api/java/io/LineNumberReader.html developer.android.com/reference/java/io/LineNumberReader.htmlMellicent
Please tell the values of n that are generated.Just
I would read lines n (which is random no) times and probably get the output, but i just wanted to check if i can work with this lesser known class.Mellicent
@Just n generates values between 1 to 500,Mellicent
J
1

Now this code works but always gives the first line. The random number is generated but the lnr (LineNumberReader) reads first line always. Why?

See this LineNumberReader

Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber().

So that's why it reading first line always.

Juetta answered 19/8, 2016 at 7:24 Comment(3)
See also on Oracle docs.oracle.com/javase/6/docs/api/java/io/LineNumberReader.html This class does not provide the function as it named.Trumpet
so i had wrong assumptions. in this case how can i change current line number being read? is skip() useful?Mellicent
@LongRanger Exactly. I am thinking to use loop with random no and read lines.Mellicent

© 2022 - 2024 — McMap. All rights reserved.