| Author |
Problem when reading a CSVs file as separate words - any help!!
|
celine scarlett
Ranch Hand
Joined: Nov 06, 2005
Posts: 93
|
|
Hi, I have a text file with lines of comma separated values, for example as follows,
Forrest Gump, Tom Hanks, Gary Sinise, Robert Zemeckis
I'm able to write this data to a file with no problem, but when I try to read back the file using standard string calls, eg: readWord, it just reads in the separate commas, words and so forth. How do I get it to read the whole line, and then separate it out to the required words? My code for the title class is as follows, Any help gratefully appreciated!!
|
 |
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html shows an example of reading a file line by line. You can then use String.split() to separate on comma values although beware of extra whitespaces if you have them after the comma as in your example.
|
My Blog: Down Home Country Coding with Scott Selikoff
|
 |
celine scarlett
Ranch Hand
Joined: Nov 06, 2005
Posts: 93
|
|
Hi, I've tried implementing the following code to fix the above problem, but I'm not having much luck. The main class now just saves to the text file '0 0'. There is now no data being saved. Any idea how I can fix this problem? I'm trying to read in a line of CSVs and separate them into indivdual strings. Any help or advice appreciated!!
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
How do you know if the readline and split even works? You don't do anything with the myStrings array. You could have done it correctly, and simply thrown it away. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
celine scarlett
Ranch Hand
Joined: Nov 06, 2005
Posts: 93
|
|
Hi, I've tried implementing the String array within the main class by itself and it outputs a line of the separate words with no problem. It removes the commas as follows,
297452Lost in TranslationBill MurrayScarlett JohanssonSofia Coppola
When I include all the previous code as follows, I get the following problem,
Error in SimpleInput.readInt() - digit expected ,Lost in Translation,Bill Murray,Scarlett Johansson,Sofia Coppola
I think the reading of the line as a string and extracting the words is working, but why is there a problem with reading in the first int in the CSV list? Any ideas how to fix this?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Any ideas how to fix this?
Unfortunately, that debugging message (or lack of it) is your code. There is just not enough information provided for us. I would suggest that you modify that error message to provide a stack trace (Just create an exception, and call the print stack method). Furthermore, I would also suggest that you don't make any assumptions. Load the code with debugging messages. Don't assume that the code broke at a certain location -- be certain of it. Henry
|
 |
celine scarlett
Ranch Hand
Joined: Nov 06, 2005
Posts: 93
|
|
Hi, I think I've managed to fix the previous error, 'digit expected', but I still get a problem when I save the new text file after adding a new title. The output after adding a new title is as follows,
Borrower Count = 2 Max Borrowers = 20 Code Number = 278342 Film = ,Forrest Lead Actor = Gump,Tom Supporting Actor = Hanks,Gary Director = Sinise,Robert Code Number = 273654 Film = Lost in Translation Lead Actor = Bill Murray Supporting Actor = Scarlett Johansson Director = Sofia Coppola Zemeckis
and the text file is saved as follows,
20 2 278342,,Forrest,Gump,Tom,Hanks,Gary,Sinise,Robert 273654,Lost in Translation,Bill Murray,Scarlett Johansson,Sofia Coppola
As shown in the above output, each time I add a new title to the list, the main class rewrites the previous titles, adding a comma after each word. I really don't understand why this is happening. Any help or advice would be gratefully appreciated!!
|
 |
 |
|
|
subject: Problem when reading a CSVs file as separate words - any help!!
|
|
|