Link to home
Start Free TrialLog in
Avatar of kousis
kousis

asked on

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
Avatar of zzynx
zzynx
Flag of Belgium image

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;
    }
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);
    }
}
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of kousis
kousis

ASKER

hi zzynx
i've tried your code.but ther entire file is printing.
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.**
Avatar of kousis

ASKER

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..   .
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.
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).
Avatar of kousis

ASKER


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
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?
Avatar of kousis

ASKER

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
Avatar of kousis

ASKER

sorry ..my output is the same as my input
Perhaps there is something wrong with the logic of 'stop' that you're implementing. Can you post the code too, anyway?
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.
Avatar of kousis

ASKER

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
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 )
  {
    ....
Important:

And you do have

 boolean busy = false;

before the while loop?
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?
Then, how does it come that when performing

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

busy turns to true for e.g. the first line...
>> Can you check in your code
We can keep busy like this...
I posted the whole class tt, let him post it too.
Avatar of kousis

ASKER

it prints the entire file and also prints
 contains end:true  at the end.
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
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();
    }
}
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...
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.
Now that you're online...
What about this question?
If mayankeagle agrees, I think I got the right answer.
So my recomm: no split.
What do you think mayankeagle?
Hmmm. You've put me into thought. I wouldn't mind it, zzynxie, but my comment at https://www.experts-exchange.com/questions/20946380/print-page-from-desired-position.html#10782194 could also be a correct solution. If you had posted your comment at {http:#10782400} before that one, I would've agreed.
My third comment was a working (and tested) one.
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.
>> I have no doubts that it will not work

I have no doubts that it will work ;-)
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
       Date: 04/07/2004 03:24PM CEST
and
        Date: 04/08/2004 01:13PM CEST
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.
Thanks.
How do I get the reference (like {http:#10782400} ) to my third comment?
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
I see. Never mind.

So,

dear moderator,
the recommandation we both agreed upon is

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