Trouble with reading and appending to lines in a file...

S

scopp

Hi there,

Please bare with me on this, I'm a newbie :eek:). 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
 
M

Michael Dunn

: Hi there,
:
: Please bare with me on this, I'm a newbie :eek:). 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();}
}
 
A

Andrew Thompson

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
 

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,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top