• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem when reading a CSVs file as separate words - any help!!

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic