This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes reading strings from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading strings from a file" Watch "reading strings from a file" New topic
Author

reading strings from a file

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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".......


SCJP
Visit my download page
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11145
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i would like to say
String x;

while (not eof)
{
x = "A" //and so on
do something
}
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2833
    
  11

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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

Joined: Oct 02, 2003
Posts: 11145
    
  16

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

Joined: Sep 21, 2006
Posts: 150

Would it be more efficient to use a Scanner?

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: reading strings from a file
 
Similar Threads
how to read file
XML File Reading
Uploading from a web page
Excel file manipulation
Read from Jar