print page from desired position

ATOM    484  CA  VAL    64       6.161   4.966  18.749  1.00 25.51           C  
ATOM    485  C   VAL    64       6.212   4.530  20.218  1.00 29.06           C  
ATOM    486  O   VAL    64       5.237   3.878  20.635  1.00 27.83           O  
ATOM    487  CB  VAL    64       6.574   3.757  17.904  1.00 25.10    
TER     534      LYS    68                                                      
HETATM  535 ZN    ZN   101      -5.889   8.020  17.081  1.00 28.92          ZN  
HETATM  536 CL    CL   102      -5.611   5.551  19.300  1.00 49.84          CL  
HETATM  537  O   HOH   103       4.601  18.810  22.009  1.00 35.24           O  
HETATM  538  O   HOH   104      16.348   9.882   9.050  1.00 38.49           O  
HETATM  539  O   HOH   105      13.273  13.044  17.973  1.00 33.83           O  
END
hi
I am having the file like mentioned above.i want to print TER till END and save it another file.      import java.lang.*;
import java.util.*;
import java.text.*;
import java.io.*;
public class tt {
    public static void main(String[] args) throws IOException
    {
      File inputFile = new File("1AB4.rtf");
      FileReader in = new FileReader(inputFile);
     File outputFile=new File("t.rtf");
      FileWriter out=new FileWriter(outputFile);
      int end;
      String s;
      boolean stop=false;
      BufferedReader br=new BufferedReader(in);
          PrintWriter pw=new PrintWriter(out);
           while((s=br.readLine())!=null)
      {
           stop = s.startsWith("END");
            if (!stop)
           
             System.out.println(s);
             else
            {
 
             end=s.indexOf("TER");
             s=s.substring (end,s.indexOf("END"));
               
              System.out.println(s);

                             break;
    }
               
              pw.write(s);
             
}
             
in.close();
out.close();
}
}
This is not giving the output of what i am expecting.
TIA
kousisAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
zzynxConnect With a Mentor Software engineerCommented:
This one works (I tested it):

public class tt {
    public static void main(String[] args) throws IOException
    {
      File inputFile = new File("c:\\In.rtf");
      FileReader in = new FileReader(inputFile);
      File outputFile=new File("c:\\Out.rtf");
      FileWriter out=new FileWriter(outputFile);
     int end;
      String s;
      boolean stop=false;
      BufferedReader br=new BufferedReader(in);
      PrintWriter pw=new PrintWriter(out);
      boolean busy = false;
      while((s=br.readLine())!=null)
      {
        stop = s.startsWith("END");
        if (stop) {
            pw.println(s);
            System.out.println(s); // comment out if you don't want the END to be printed out
            break;
        }
        else
        {
            busy = busy || (s.indexOf("TER")!=-1);
            if (busy) {
               System.out.println(s);
               pw.println(s);
            }
        }
      }
         
      in.close();
      out.close();
    }
}
0
 
zzynxSoftware engineerCommented:
while((s=br.readLine())!=null)
      {
           stop = s.startsWith("END");
            if (!stop)
           
             System.out.println(s);       /// <<<<<<<<<<< This one prints *each* line
             else
            {
 
             end=s.indexOf("TER");
             s=s.substring (end,s.indexOf("END"));
               
              System.out.println(s);

                             break;
    }
0
 
zzynxSoftware engineerCommented:
Is this what you want?

while((s=br.readLine())!=null) {
    boolean busy = false;
    stop = s.startsWith("END");
    if (stop) {
       System.out.println(s); // comment out if you don't want the END to be printed out
       break;
    }
    else
    {
        busy = busy || s.indexOf("TER");
        if (!busy)
          continue;
        System.out.println(s);
    }
}
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
zzynxSoftware engineerCommented:
Output file:

TER     534      LYS    68                                                      
HETATM  535 ZN    ZN   101      -5.889   8.020  17.081  1.00 28.92          ZN  
HETATM  536 CL    CL   102      -5.611   5.551  19.300  1.00 49.84          CL  
HETATM  537  O   HOH   103       4.601  18.810  22.009  1.00 35.24           O  
HETATM  538  O   HOH   104      16.348   9.882   9.050  1.00 38.49           O  
HETATM  539  O   HOH   105      13.273  13.044  17.973  1.00 33.83           O  
END
0
 
kousisAuthor Commented:
hi zzynx
i've tried your code.but ther entire file is printing.
0
 
zzynxSoftware engineerCommented:
Sure you copied/pasted it correctly?
I don't think so. For me it's working perfectly.
The printing output as well as the t.rtf output file both look as my previous comment.

**Can you make sure to copy/paste and try it once again, please.**
0
 
kousisAuthor Commented:
I have done it once again.. but i am getting entire file.
as per your code

        stop = s.startsWith("END");
        if (stop) {
            pw.println(s);
         }
prints the whole sequence..isn't it..
it would be better if u explain me how it works..   .
0
 
zzynxSoftware engineerCommented:
Can't believe my eyes/ears.

>> it would be better if u explain me how it works..
OK, here we go. See the comments I added.

public class tt {
    public static void main(String[] args) throws IOException
    {
      File inputFile = new File("c:\\In.rtf");
      FileReader in = new FileReader(inputFile);
      File outputFile=new File("c:\\Out.rtf");
      FileWriter out=new FileWriter(outputFile);
     int end;
      String s;
      boolean stop=false;
      BufferedReader br=new BufferedReader(in);
      PrintWriter pw=new PrintWriter(out);
      boolean busy = false;                                        // have we found the line with "TER" yet?
      while((s=br.readLine())!=null)                            // read line by line
      {
        stop = s.startsWith("END");                              // search for the line starting with "END" == the last line
        if (stop) {                                                       // the last line found
            pw.println(s);                                             // write to file that last line
            System.out.println(s);                                 // print out that last line
            break;                                                       // we're done processing the input file
        }
        else                                                              // we're not at the last line yet
        {
            busy = busy || (s.indexOf("TER")!=-1);       // Is this line the one that contains "TER"?
            if (busy) {                                                 // Is the line with "TER" already found?
               System.out.println(s);                             // then print out this line (=one that comes after the line with "TER")
               pw.println(s);                                         // write this line to the output file
            }
        }
      }
         
      in.close();
      out.close();
    }
}

So as long as the line containting "TER" isn't encountered, nothing is printed.
I hope it is clear now.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Well, maybe if kousis can post the entire code that he is using, we could be able to help out better (there might be something different from the code that zzynx posted, I guess).
0
 
kousisAuthor Commented:

thanx for your explanation. zzynx I am using the same code as zzynx mentioned. but i am getting the different output. i am confused how output changes with the same program.
regards
kousis
0
 
zzynxSoftware engineerCommented:
My input file is what I copied from your question (everything except the []):

[ATOM    484  CA  VAL    64       6.161   4.966  18.749  1.00 25.51           C  
ATOM    485  C   VAL    64       6.212   4.530  20.218  1.00 29.06           C  
ATOM    486  O   VAL    64       5.237   3.878  20.635  1.00 27.83           O  
ATOM    487  CB  VAL    64       6.574   3.757  17.904  1.00 25.10    
TER     534      LYS    68                                                      
HETATM  535 ZN    ZN   101      -5.889   8.020  17.081  1.00 28.92          ZN  
HETATM  536 CL    CL   102      -5.611   5.551  19.300  1.00 49.84          CL  
HETATM  537  O   HOH   103       4.601  18.810  22.009  1.00 35.24           O  
HETATM  538  O   HOH   104      16.348   9.882   9.050  1.00 38.49           O  
HETATM  539  O   HOH   105      13.273  13.044  17.973  1.00 33.83           O  
END]

>> I am using the same code as zzynx mentioned
To be sure, can you nevertheless post it?

>> but i am getting the different output
Can you post your output?
0
 
kousisAuthor Commented:
My inpu  is the same as input file.the following is my output
ATOM    484  CA  VAL    64       6.161   4.966  18.749  1.00 25.51           C  
ATOM    485  C   VAL    64       6.212   4.530  20.218  1.00 29.06           C  
ATOM    486  O   VAL    64       5.237   3.878  20.635  1.00 27.83           O  
ATOM    487  CB  VAL    64       6.574   3.757  17.904  1.00 25.10    
TER     534      LYS    68                                                      
HETATM  535 ZN    ZN   101      -5.889   8.020  17.081  1.00 28.92          ZN  
HETATM  536 CL    CL   102      -5.611   5.551  19.300  1.00 49.84          CL  
HETATM  537  O   HOH   103       4.601  18.810  22.009  1.00 35.24           O  
HETATM  538  O   HOH   104      16.348   9.882   9.050  1.00 38.49           O  
HETATM  539  O   HOH   105      13.273  13.044  17.973  1.00 33.83           O  
END
0
 
kousisAuthor Commented:
sorry ..my output is the same as my input
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Perhaps there is something wrong with the logic of 'stop' that you're implementing. Can you post the code too, anyway?
0
 
zzynxSoftware engineerCommented:
Unbelievable!

Can you please alter the two

        System.out.println(s);

into once

        System.out.println("**** "+ s);

and once

        System.out.println("==== "+ s);

What is your output then?
This is to see which one is responsible for the printing of the lines we don't want to be printed.
0
 
kousisAuthor Commented:
while((s=br.readLine())!=null)                            
      {
        stop=s.startsWith("END");                              
        if (stop) {                                                      
            pw.println(s);                                        
            System.out.println("****"+s);                                
            break;                                                      
        }
        else                                                            
        {
            busy = busy || (s.indexOf("TER")!=-1);      
            if (busy) {                                              
               System.out.println("-----"+s);                            
               pw.println(s);                                        
            }
        }
      }
i am getting the output of entire file with
------HETATM
-------HTATM and ends with
****END
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Well, I know that zzynx can't believe it. Neither can I.

Maybe you could try:

stop = s.startsWith ( "END" ) ;
System.out.println ( s + " CONTAINS END: " + stop ) ;      

-> just see the value that it prints.

I suggest the use of:

while ( ( s = br.readLine () ) != null )                            
{
  if ( s.indexOf ( "END" ) == 0 )
  {
    ....
0
 
zzynxSoftware engineerCommented:
Important:

And you do have

 boolean busy = false;

before the while loop?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Can you check in your code if you have a System.out.println ( s ) ; statement somewhere inside the while loop but outside the if/ else cases, by mistake?
0
 
zzynxSoftware engineerCommented:
Then, how does it come that when performing

       busy = busy || (s.indexOf("TER")!=-1);

busy turns to true for e.g. the first line...
0
 
zzynxSoftware engineerCommented:
>> Can you check in your code
We can keep busy like this...
I posted the whole class tt, let him post it too.
0
 
kousisAuthor Commented:
it prints the entire file and also prints
 contains end:true  at the end.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Or try this:

int start = -1 ;

while ( ( s = br.readLine () ) != null )
{
  if ( s.startsWith ( "END" ) )
  {
    pw.println ( s ) ;
    break ;

  } // end if

  start = ( start >= 0 ) ? start : s.startsWith ( "TER" ) ;
 
  if ( start >= 0 )
    pw.println ( s ) ; // end if
 
} // end while
0
 
zzynxSoftware engineerCommented:
Can you post the output of this code (added some extra tracing):

public class tt {
    public static void main(String[] args) throws IOException
    {
      File inputFile = new File("c:\\In.rtf");
      FileReader in = new FileReader(inputFile);
      File outputFile=new File("c:\\Out.rtf");
      FileWriter out=new FileWriter(outputFile);
     int end;
      String s;
      boolean stop=false;
      BufferedReader br=new BufferedReader(in);
      PrintWriter pw=new PrintWriter(out);
      boolean busy = false;                                        // have we found the line with "TER" yet?
      while((s=br.readLine())!=null)                            // read line by line
      {
        stop = s.startsWith("END");                              // search for the line starting with "END" == the last line
        System.out.println("ReadLine: <"+ s + "> stop= "+ stop);  // <<<<<<<< EXTRA Tracing >>>>>>>>>>
        if (stop) {                                                       // the last line found
            pw.println(s);                                             // write to file that last line
            System.out.println(s);                                 // print out that last line
            break;                                                       // we're done processing the input file
        }
        else                                                              // we're not at the last line yet
        {
            System.out.println("[1] busy = " + busy);  // <<<<<<<< EXTRA Tracing >>>>>>>>>>
            busy = busy || (s.indexOf("TER")!=-1);       // Is this line the one that contains "TER"?
            System.out.println("[2] busy = " + busy);  // <<<<<<<< EXTRA Tracing >>>>>>>>>>
            if (busy) {                                                 // Is the line with "TER" already found?
               System.out.println(s);                             // then print out this line (=one that comes after the line with "TER")
               pw.println(s);                                         // write this line to the output file
            }
        }
      }
         
      in.close();
      out.close();
    }
}
0
 
zzynxSoftware engineerCommented:
Another point I was thinking of...
Are you sure you run the code you think you are running?
In other words make sure that the class file you are running corresponds with the code we see here.
(Delete your class file and recompile/rebuild.
 Or rename your class from "tt" to "ZzynxDemo" ;)
Just a thought...
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Hmmm.... you can try that. You can also try the code which I posted in my last comment and let me know if it worked.
0
 
zzynxSoftware engineerCommented:
Now that you're online...
What about this question?
0
 
zzynxSoftware engineerCommented:
If mayankeagle agrees, I think I got the right answer.
So my recomm: no split.
What do you think mayankeagle?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Hmmm. You've put me into thought. I wouldn't mind it, zzynxie, but my comment at http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20946380.html#10782194 could also be a correct solution. If you had posted your comment at {http:#10782400} before that one, I would've agreed.
0
 
zzynxSoftware engineerCommented:
My third comment was a working (and tested) one.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I know, zzynx. I have no doubts that it will not work. But the comment I posted at {http:#10782194} would also work. Just that I did not provide full-code but only the main loop.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I have no doubts that it will not work

I have no doubts that it will work ;-)
0
 
zzynxSoftware engineerCommented:
You just said:
>> If you had posted your comment at {http:#10782400} before that one, I would've agreed.
Well, my *third* comment is posted long before that one
0
 
zzynxSoftware engineerCommented:
       Date: 04/07/2004 03:24PM CEST
and
        Date: 04/08/2004 01:13PM CEST
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Ah, I didn't notice that was your third comment. I thought you were referring to the one which Venabili mentioned, at: {http:#10782400}

Ok, I agree.
0
 
zzynxSoftware engineerCommented:
Thanks.
How do I get the reference (like {http:#10782400} ) to my third comment?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can't. Page editors and moderators have a different view where they can see the comment-IDs, but we can't see it in our view. THe only time we can capture the comment ID of a comment is perhaps when we get a mail-alert for that one (because the link there comes along with the comment-ID), like: http://www...../..../Java/Q_20946380.html#11047744
0
 
zzynxSoftware engineerCommented:
I see. Never mind.

So,

dear moderator,
the recommandation we both agreed upon is

       Accept: zzynx (third comment on this Q)
0
 
zzynxSoftware engineerCommented:
What would EE-life be easy if all authors closed their Q's...
0
All Courses

From novice to tech pro — start learning today.