aspose file tools*
The moose likes Java in General and the fly likes Help please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help please" Watch "Help please" New topic
Author

Help please

Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
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

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Feb 03, 2001
Posts: 142
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

Joined: Feb 03, 2001
Posts: 142
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

Joined: Jan 09, 2001
Posts: 1492
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help please