# Tokens and Java, the last cry for assistance



## Atnevon (Aug 8, 2008)

Hello again TPU,

I need a bit of help for my last project for my Java class. 

Basicly, I want to do this in a nutshell:

We are to take a txt file of employee data. This is similar to my last project, but this time only involves String manipulation and no arithmetic.

What I want to to is read a file (ill post it in a sec). From there, I want the file to read the info, and make an array (or arraylist). I want the data to be all Strings. THe data will fill the rows as seperated only by the Space key (spacebar), and a new row will be made when a newline is created.

Once done, I want to only change the data in one column. This column will change one String to another. I'm not sure how though, I'm sure I can make a combination with for loops and if statements. I just don;t know how I would compose it though.

Once done, I just want to print out the results on the screenm and at the same time, have them written to a new txt file.




Great, I have a great blueprint f how to do it. The only trouble is the writing part, and what would make me a shitty programmer. 

I desperatly need some help here. If anyone could spare a bit of post time and knowledge of how to do this I would be most grateful.

For reference, here is the info I am workign with. You can paste it to a txt file for the same thing.



Read file said:


> Jane Rivers 902-A 05/16/2001 1 16.25
> Bob Cox 823-S 06/21/1990 2 17.50
> Ann Ramsey 715-A 02/12/1998 1 16.25
> Joseph Chandler 723-P 12/22/2000 3 14.35
> ...



From the Read File, I just need the 1,2, and 3 re-written in Column 4 to be 1 for Morning, 2 for Swing, and 3 for Night. THis would then be rewritten and displayed in a new file as this:



Output File said:


> Jane Rivers 902-A Accounting 05/16/2001 Morning Shift 16.25
> Bob Cox 823-S Shipping 06/21/1990 Swing Shift 17.50
> Ann Ramsey 715-A Accounting 02/12/1998 Morning Shift 16.25
> Joseph Chandler 723-P Production 12/22/2000 Night Shift 14.35
> ...



I just don't know how I would write this code though. This is where I need a bit of hand holding, but not do it for me. Thats what greedy people do, and I actually want to learn something from this experience.

If you can offer any help, I thank you millions.

-Andrew


----------



## Oliver_FF (Aug 8, 2008)

Here's everything you can do with a String object:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

Here's what I think is the best solution:

1. Start by reading an entire line into one String object, eg "Jane Rivers 902-A 05/16/2001 1 16.25"
2. Split the string by the " " character, giving you an array of substrings, eg "Jane" "Rivers" "902-A" "05/16/2001" "1" "16.25"
3. Swap the 4th substring with whatever you want
4. Concatenate all the substrings together again
5. Output the line to file
6. Read the next line of input, go to 2.

Dig around in the String API, it's pretty simple - if you use any loop you're probably not using the API properly


----------

