• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help please

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic