Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid/Java - Random Read line not changing
    primarykey
    data
    text
    <p>My text file contains a known number of lines (the first line of the file is the number of lines). I want to randomly read a line from the file - to do this I use <code>LineNumberReader</code>. The problem is, that it doesn't generate a new string - the random number changes but the string it gets from the <code>LineNumberReader</code> doesn't.</p> <p>As the title implies this is an Android app. <code>textbox</code> is the output area, <code>testbox</code> is for debugging (in this code it displays the random number - to check that it has changed) and <code>r</code> is a <code>Random</code></p> <p>This sample code is the <code>onClick</code> for the only button in the app.</p> <pre><code> String fileName = "\\sdcard\\Q's.txt"; try { BufferedReader br = new BufferedReader( new InputStreamReader( new DataInputStream( new FileInputStream(fileName)))); // Get the number of lines in the text file numLines = Integer.parseInt(br.readLine().toString().trim()); // get a random number and write it to the test text box int rnd = r.nextInt(numLines); String tmp = Integer.toString(rnd); testBox.setText(tmp); // get random number - go to line - save in String "text" LineNumberReader rdr = new LineNumberReader(br); rdr.setLineNumber(r.nextInt(numLines)); textBox.setText(rdr.readLine()); } catch (FileNotFoundException e) { textBox.setText("FileNotFound"); e.printStackTrace(); } catch (IOException e) { textBox.setText("IOException"); e.printStackTrace(); } catch (NumberFormatException nfe){ textBox.setText("Number Formated Wrong"); } finally {} </code></pre> <p>What am I doing wrong? I have tried closing and reseting <code>br</code> and <code>rdr</code>and can't seem to get it to work.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload