Trouble with reading and appending to lines in a file...  
Author Message
scopp





PostPosted: 2004-1-22 9:29:00 Top

java-programmer, Trouble with reading and appending to lines in a file... Hi there,

Please bare with me on this, I'm a newbie :o). Ok, I'm trying to read a
file (a bunch of 1 word lines), find the last line with the word "SKIP" at
the end, and get the next line. After getting the desired line, I want to
append the word "SKIP" to that line as well and save and close the file.

I also want the class to have an input parameter (integer), which will be
the same name of the file (example: input = 1234 --> filname = 1234.txt).

Here is what I've got so far:

import java.io.*;
public class GetLine {
int number; //let's say the lines in the file are all 5 digit
numbers, with no spaces
public boolean evaluate(String str) {
//
//I'm not sure how to evaluate if the end of the line
has the word SKIP and the following doesn't work????
//
if ( StringBuffer.indexOf("SKIP\n") )
return true;
}

public void get(int c) throws IOException, FileNotFoundException {
String line;
boolean b;
FileReader fr=new FileReader(number + ".txt");
BufferedReader br=new BufferedReader(fr);
while((line=br.readLine())!=null )
{
b = evaluate(line);
if (b==false)
{
//
//not sure if I can just do a "return
line" to get the first line found without SKIP???
return line;
//
//not sure if I can do the following
either???
//
line = line + "SKIP";

}
}
}
}


Please, any suggestions are apprciated. Thanks for your help.

scopp


 
Michael Dunn





PostPosted: 2004-1-22 11:17:00 Top

java-programmer >> Trouble with reading and appending to lines in a file...
"scopp" <email***@***.com> wrote in message news:email***@***.com...
: Hi there,
:
: Please bare with me on this, I'm a newbie :o). Ok, I'm trying to read a
: file (a bunch of 1 word lines), find the last line with the word "SKIP" at
: the end, and get the next line. After getting the desired line, I want to
: append the word "SKIP" to that line as well and save and close the file.
:
: I also want the class to have an input parameter (integer), which will be
: the same name of the file (example: input = 1234 --> filname = 1234.txt).
:
: Here is what I've got so far:
:
: import java.io.*;
: public class GetLine {
: int number; //let's say the lines in the file are all 5 digit
: numbers, with no spaces
: public boolean evaluate(String str) {
: //
: //I'm not sure how to evaluate if the end of the line
: has the word SKIP and the following doesn't work????
: //
: if ( StringBuffer.indexOf("SKIP\n") )
: return true;
: }
:
: public void get(int c) throws IOException, FileNotFoundException {
: String line;
: boolean b;
: FileReader fr=new FileReader(number + ".txt");
: BufferedReader br=new BufferedReader(fr);
: while((line=br.readLine())!=null )
: {
: b = evaluate(line);
: if (b==false)
: {
: //
: //not sure if I can just do a "return
: line" to get the first line found without SKIP???
: return line;
: //
: //not sure if I can do the following
: either???
: //
: line = line + "SKIP";
:
: }
: }
: }
: }
:
:
: Please, any suggestions are apprciated. Thanks for your help.
:
: scopp


Something to play around with


import java.io.*;
import java.util.ArrayList;
class TestInputOutput
{
public TestInputOutput()
{
try
{
ArrayList lines = new ArrayList();
BufferedReader fr = new BufferedReader(new FileReader("Test.txt"));
String line = "";
while((line = fr.readLine()) != null) lines.add(line);
for(int x = lines.size()-1; x >= 0; x--)
{
line = lines.get(x).toString();
if(line.substring(line.length()-4).equals("SKIP"))
{
if(x < lines.size()-1) lines.set(x+1,lines.get(x+1) + "SKIP");
break;
}
}
fr.close();
FileWriter fw = new FileWriter("Test.txt");
for(int x = 0; x < lines.size(); x++)
{
fw.write(lines.get(x).toString() + "\r\n");
}
fw.close();
}
catch(Exception e){System.out.println("error - terminating");}
System.exit(0);
}
public static void main(String[] args) {new TestInputOutput();}
}


 
Andrew Thompson





PostPosted: 2004-1-22 11:38:00 Top

java-programmer >> Trouble with reading and appending to lines in a file... "scopp" <email***@***.com> wrote in message
news:email***@***.com...

I suspect Michael's answer will fix the
immediate problem, but I would like to
address a more fundamental issue.

Techniques for testing..
....
| Here is what I've got so far:

An example, you are already miles ahead
of the game.

| import java.io.*;
| public class GetLine {
| int number; //let's say the lines in the file
are all 5 digit
| numbers, with no spaces
| public boolean evaluate(String str) {
| //
| //I'm not sure how to evaluate if the end
of the line
| has the word SKIP and the following doesn't work????
| //

But here is where you should insert some
code if things are not working..

System.out.println( "GL.e str: '" + str + "'");

Note that I have put single quotes
either side of the actual string..

Give it a try and see if it reveals
anything significant..

| if ( StringBuffer.indexOf("SKIP\n") )
| return true;
| }
......

One last comment is that GetLine is
a bad name for a class, classes should
be nouns rather than verbs..

HTH


 
 






PostPosted: 2004-1-23 2:06:00 Top

java-programmer >> Trouble with reading and appending to lines in a file... Both of your suggestions have been duley noted. Thank you both very much!

stephen


"Andrew Thompson" <email***@***.com> wrote in message
news:2AHPb.22880$email***@***.com...
> "scopp" <email***@***.com> wrote in message
> news:email***@***.com...
>
> I suspect Michael's answer will fix the
> immediate problem, but I would like to
> address a more fundamental issue.
>
> Techniques for testing..
> ....
> | Here is what I've got so far:
>
> An example, you are already miles ahead
> of the game.
>
> | import java.io.*;
> | public class GetLine {
> | int number; //let's say the lines in the file
> are all 5 digit
> | numbers, with no spaces
> | public boolean evaluate(String str) {
> | //
> | //I'm not sure how to evaluate if the end
> of the line
> | has the word SKIP and the following doesn't work????
> | //
>
> But here is where you should insert some
> code if things are not working..
>
> System.out.println( "GL.e str: '" + str + "'");
>
> Note that I have put single quotes
> either side of the actual string..
>
> Give it a try and see if it reveals
> anything significant..
>
> | if ( StringBuffer.indexOf("SKIP\n") )
> | return true;
> | }
> ......
>
> One last comment is that GetLine is
> a bad name for a class, classes should
> be nouns rather than verbs..
>
> HTH
>
> --
> Andrew Thompson
> * http://www.PhySci.org/ PhySci software suite
> * http://www.1point1C.org/ 1.1C - Superluminal!
> * http://www.AThompson.info/andrew/ personal site
>
>