Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help please

 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a snippet of code. I am able to generate a random line number and read four lines in
a row (they represtent 4 multiple choice answers). When I try to place them into an array,
however, I always get the first 20 elements in the file, not the line numbers I want.
Why am I not getting the proper line numbers loaded into my array?
Thanks in advance.

Here's the whole thing....

[This message has been edited by Brian Snyder (edited March 01, 2001).]
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,
Your problem is that LineNumberReader is not a random access input stream. The only thing it provides over BufferedReader is that each line gets assigned a line number (after reading it in!). I don't think it really helps you in your cause. You would be better off reading entire file contents using BufferedReader and place into vector. Once you get whole file in memory then use toArray() method on vector to get array. Then go ahead and perform your random selections on the array.
Good Luck,
Manfred.
 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manfred,
That was a big help. I didn;t realize it was so easy to read a file into an array. I had to tinker with Ebumeration to separate the file as with StringTokenizer. See below.
Thanks again!!!
 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It now work wonderfully. It reads from a file whose numbers 1 through 78 are typed one line at a time. It chooses 5 random questions without repetition, and knows the answers to those four. Thanks Manfred!!!
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,
Glad that worked. Looking at your code I think you can clean it up a lot. The part with the enumeration is not required (I think!). Lets say that I have a loop which places all strings into a vector:
Vector allStringsVector = new Vector();
for( int i = 0; i < x; ++i )
{
AllStringsVector.addElement( string[i] );
}
Then I can use the toArray() method to get an array of Strings back using the cast operator:
String[] stringArray = (String[])allStringsVector.toArray();
Then we can go ahead and use the stringArray.
Enjoy,
Manfred.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic