• Post Reply Bookmark Topic Watch Topic
  • New Topic

StringTokenizer  RSS feed

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using FileReader, FileWriter and their constituents, I am creating a file to contain employee information ( name, age, hours, etc. ). The user is to input all of the data on a single line and we were asked to implement StringTokenizer to assign that data to the file. I have never used the StringTokenizer before, but I have a rough idea how it is to function. I used pw.println to test what I have so far, now I would like to let the user build the initial file with the "first employees" of the company, and then view the file, and then go back and append new employee data to that same file. My question is, how can I take the user input as a StringTokenizer and add that to the file?

In the for loop below, I thought I would see if it would work, but it does not. The loop only executes once and does not allow me to enter data.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Smiths wrote: . . . I have never used the StringTokenizer before, . . .
And I hope you will never use it again. Tokenizer is marked in the API documentation as legacy code which should not be used in new code. If you are reading a text file use a Scanner and its nextXXX methods. That depends on your knowing the structure of the file, of course. But you can hardly use a Tokenizer without knowing the strcuture of the file either.

But you have been told to use Tokenizer, so you will have to use it. What it does is take a String (i.e. your line) and divide it into parts. You will find a simple example in the link I posted earlier. Unlike a Scanner which returns all sorts of different things, a tokenizer only ever returns a String. You may need to parse this String to a number.
Don't call close() on readers and writers. Yes, you must close them (unless they point to one of the Streams in the System class), but it is better to use try‑with‑resources, but that only works in Java7+. If you are using an older version you would have to use try‑finally, in which case you will have to call close().
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you been told about Scanners and nextLine? What does your book say nextLine does? Because nextLine confuses a lot of people. It is liable to go horribly wrong if you use it after nextSomethingElse.

You can read about nextLine here.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And I hope you will never use it again. Tokenizer is marked in the API documentation as legacy code which should not be used in new code. If you are reading a text file use a Scanner and its nextXXX methods. That depends on your knowing the structure of the file, of course. But you can hardly use a Tokenizer without knowing the strcuture of the file either.

But you have been told to use Tokenizer, so you will have to use it. What it does is take a String (i.e. your line) and divide it into parts. You will find a simple example in the link I posted earlier. Unlike a Scanner which returns all sorts of different things, a tokenizer only ever returns a String. You may need to parse this String to a number.
Don't call close() on readers and writers. Yes, you must close them (unless they point to one of the Streams in the System class), but it is better to use try‑with‑resources, but that only works in Java7+. If you are using an older version you would have to use try‑finally, in which case you will have to call close().


Mr. Campbell - or rather, JAVA JEDI : I'm most familiar with using Scanner and it pains me to hear that Tokenizer has been officially dropped since I'm personally running low on memory and don't have room for applications no longer accepted by the community. The file that we are to create is a .java file (is this for simplicity?) to hold the employee data. I see how you create a StringTokenizer explicitly, but I don't know how to do that from a Scanner input? We were asked to use the Tokenizer to be the type of the console input. Apparently, it is supposed to make it easier to build the file..?

The nextLine() method in our book is defined as, "reads a line of text (i.e. a string ending with the ENTER key pressed)". We're required, however, per the assignment, to use the classes I have in the assignment.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which book?

Have a look at what it says in your book and then compare it with the official line on Scanner#nextLine. I don't see that many books, but I have yet to see one that gives complete info about nextLine. The link I gave you earlier will explain why your entry appears empty.I have no idea why you are putting Employee data into a .java file. Please check the instructions carefully to be sure you have got that bit right.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book is Introduction to Java by Daniel Liang. This book doesn't do a very good job of explaining methods at all. Just shows a bunch of examples of the code being used. I'm sure that typical of programming books tho.

I will check the link. Thank you.

I'm not to sure either, but that is what the assignment called for. (?)


 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the assignment says .java file, that is what you will have to do, but I would query it with the instructor before handing it in. If it does turn out to be a mistake you can probably correct it quite quickly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!