This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read all words from a text file into an array

 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

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.




Thanks in advance!
 
Joe Ess
Bartender
Posts: 9311
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Any takers?
 
Joe Ess
Bartender
Posts: 9311
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
Joe Ess
Bartender
Posts: 9311
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Angela Truce:
But for some reason it won't work:


I'll say. That code won't even compile. Can you give us some code that will compile, then tell us what you expect it to do and what it actually does?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic