simple logic question

S

Sue

Hi, I have a text file of the foll format

date <end line>
some text <end line>
some more text <end line>
date <end line>
some text <end line>
more text<end line>
date

I am using Datainputstream readLine to read line by line -- how can I
tell the program to start reading from one date and stop when the next
date is found and put everything from one date to the next in one line?
Example, I need
date some text some more text <end line>
date some text more text <end line>
and so on... thank you very much for helping.
 
J

jeff.bethune

Well using the readline is still fine, but just do a couple readlines.
If the data format of the text file never changes, you could simply
cheat by doing three readlines and concatenating the results together.
Or if you have an arbitrary number of readlines just read lines in and
concatenate until a line with date characteristics is reached
(01/01/2005, could cue on the slashes or perhaps on the short length of
the line). So something like this (note that
DataInputStream.readLine() is deprecated so use BufferedReader
instead):

BufferedReader reader = new BufferedReader(new FileReader(text.txt));
String line = "";
String concat = "";

while( (line = reader.readLine()) != null) {
//grab 3 character and see if it's a slash
if(line.charAt(2) == '/'){
//ensure not empty, ie. on first pass thru here
if(concat != "") {
//append line end like you wanted
concat = concat + "\n";

//do something with full text between dates
}

//restart with date information
concat = line;
} else {
//concat information
concat = concat + " " + line;
}
}


I'm sure there's other ways and better code lol, but this is just to
give you an idea.

-Jeff
 
V

Vincent H

Hi, I have a text file of the foll format

date <end line>
some text <end line>
some more text <end line>
date <end line>
some text <end line>
more text<end line>
date

I am using Datainputstream readLine to read line by line -- how can I
tell the program to start reading from one date and stop when the next
date is found and put everything from one date to the next in one line?
Example, I need
date some text some more text <end line>
date some text more text <end line>
and so on... thank you very much for helping.

You could try the following. First create an empty StringBuffer. Then read the inputstream line by line. For each line
that you have read check if it is a date. It it is then append an new-line to the StringBuffer. After that (whether or
not you've read a date) append the line you've read to the StringBuffer. In the end you'll have the result you want.
This will result however in a StringBuffer that starts with a new-line (when the file starts with a date). If you don't
want that you'll have to check that specific case.

Hope this helps,

Vincent
 
M

Matt Humphrey

Sue said:
Hi, I have a text file of the foll format

date <end line>
some text <end line>
some more text <end line>
date <end line>
some text <end line>
more text<end line>
date

I am using Datainputstream readLine to read line by line -- how can I
tell the program to start reading from one date and stop when the next
date is found and put everything from one date to the next in one line?
Example, I need
date some text some more text <end line>
date some text more text <end line>
and so on... thank you very much for helping.

First, you have to assume that the text will never look like a date. You
also need to specify what a date looks like using the DateFormat system.
Beyond that, the way it works is that you have a date followed by zero or
more lines which are then followed by another date. The lines are
concatenated into a single StringBuffer, but you won't know that you're done
until you receive either the next date or the end of the file. You also
have to figure out what to do if you receive a date followed by another date
(zero lines) or a date at the end of the file. (If line collector is null,
there were no lines since the last date, otherwise it contains the most
recent lines.)

Essentially this algorithm is always ready to add another string onto a line
buffer. If the string can be parsed into a date, the waiting line is
complete and can be processed along with the next date. The line collector
then restarts to collect more lines. Any line that doesn't parse is added
to the end of the collector. The code is for illustrative purposes only and
won't compile.

SimpleDateFormat sdf = new SimpleDateFormat ("MM/DD/YYYY");
BufferedInputReader br = new BufferedInputReader (...);
String line = br.readLine ();
StringBuffer lineCollector = null;
while (line != null) {
try {
Date d = sdf.parse (line);
// You have received a date. This terminates whatever prior
// sequence of lines you were working on and restarts a new one
if (lineCollector != null) {
// Do what you want with the prior sequence of lines
lineCollector = null;
}

// Do what you want with the date

} catch (FormatException ex) {
if (lineCollector == null) {
// this is the first line of the sequence
lineCollector = new StringBuffer ();
}
lineCollector.append (line);
}
line = br.readLine ();
}
// The lineCollector has the last line (if any) here.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
T

Thomas Weidenfeller

Sue said:
Hi, I have a text file of the foll format

date <end line>
some text <end line>
some more text <end line>
date <end line>
some text <end line>
more text<end line>
date

I am using Datainputstream readLine

DataInputStream is most likely the wrong class for this. In addition,
DataInputStream.readLine() is deprecated. You probably want a Reader
implementation like BufferedReader, not an InputStream implementation.

Second, you need some way to identify a date, e.g. with a regular
expression or some date parser (like SimpleDateFormat). Then you can
write a loop like.

BufferedReader br = new BufferedReader( ... ) ;
StringBuffer sb = null; // StringBuilder in 1.5
String line;
while((line = br.readLine()) != null) {
if( /* check if line is a date */ ) {
if(sb != null) {
System.out.println(sb);
}
sb = new StringBuffer(line);
} else if(sb != null) {
sb.append(' ').append(line);
} else {
/* some error message - text without date */
}
}
if(sb != null) {
System.out.println(sb);
}


/Thomas
 
R

ridvansg

Queue queLines=new Queue();
String strLine=null;
while( (strLine=readLine())!=null ){
if( isDate(strLine) && !queLines.isEmpty() ) {
queLines.printContentAndEmpty();
}
queLines.add(strLine);
}
queLines.printContentAndEmpty();

Hi I know this is not a valid Java code but I think you will catch the
idea
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top