Does anyone know how to rewrite the following code to read all the words from a text file since I've run it and it only reads the last line. I want to avoid using Lists since I have no knowlegde of how to use them in the code.
Welcome to the JavaRanch, Angela. Your problem is here:
You read in a line and set array to point to the results of a split of that line, therefore array will only hold the last line in a file. A list would be the easiest way to deal with this. Otherwise you need to resize array and append the results of input.split to the previous contents. It's not difficult, just tedious.
Ok, Say I had to rewrite the code. But only using knowledge of StringTokenizer and BufferedReaders, I have to read words from a text file consisting of several lines of several words, then put them into an array. Then, using a random number generator, select a random word from this array and store as a string.
Angela, this sounds an awful lot like a homework problem. Why don't you tackle your assignment in manageable parts. First, solve your problem above: how to read in the file. We'd be glad to help you with any specific problems you have but in the end, one learns how to program by writing lots of code and we're not about to deprive you of that opportunity.
Joined: Nov 30, 2005
I have written some of the code, just wanted to compare so that I can find the problem. This part of the code is trying to read words from a text file and putting them all in the string array. But for some reason it won't work: