• 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

reading strings from a file

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know how to read a line from a text file, but how do i deal with a file like this? i want to read one string at a time
"A","ABILITY","ABLE","ABOUT","ABOVE","ABSENCE","ABSOLUTELY","ACADEMIC","ACCEPT","ACCESS","ACCIDENT","ACCOMPANY","ACCORDING","ACCOUNT".......
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as always, first things first. How do you define a string?

My guess is you can't. You may have to read it a character at a time and stop when you get to whatever is your delimiter, or read the entire thing and parse it, or use something else (like sed or awk) to break it apart first...

Why do you need to read one string at a time?
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would like to say
String x;

while (not eof)
{
x = "A" //and so on
do something
}
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remember doing something like this when I was working on Euler problems. I was working with Ruby and Python, neither of which I understood well, but the concepts should be the same. I remember two basic approaches:

Read a line
Break it up on the comma delimiter
Remove quotes

or

Read a line
Remove first and last characters (both quotation marks)
Break remaining string up on the "," delimiter


The first seems more elegant somehow, but the second might be faster.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it seems the entire file is one line "A","ABILITY","ABLE",..."YOURSELF","YOUTH"
it seems to me keeping the quotes would be a good thing.
perhaps you are right though.
btw it is Euler42 i am working on.

perhaps if i do this
String[] values = bufferedReader.readLine().split(",");
for(int i = 0; i < values.length; i++)
{
String x = values[i];
do something
}
or do i have to remove the quotes?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
personally, i would argue that it doesn't matter. the purpose is to compute the score of each word and see if it is a triangle number.

I don't see any reason why you couldn't do some pre-processing on the input file. I would personally run it through something that replaces the "," with a newline, delete any extraneous double quotes, and then eyeball the file to make sure it is what I wanted.

THEN i'd just read it in one line at a time.
 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it be more efficient to use a Scanner?

 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill, i think you may be right. i don't know scanner class....yet. it could be a good fit for this problem.
it seems i had to remove the quotes. the stage is almost set now.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic