Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading strings from a file

 
Randall Twede
Ranch Hand
Posts: 4442
3
Java
  • Mark post as helpful
  • send pies
  • 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".......
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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: 4442
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would like to say
String x;

while (not eof)
{
x = "A" //and so on
do something
}
 
Greg Charles
Sheriff
Posts: 2987
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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: 4442
3
Java
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be more efficient to use a Scanner?

 
Randall Twede
Ranch Hand
Posts: 4442
3
Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic