We help IT Professionals succeed at work.

print page from desired position

kousis
kousis asked
on
Medium Priority
305 Views
Last Modified: 2010-03-31
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
Comment
Watch Question

zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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;
    }
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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);
    }
}
Sr. Software engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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

Author

Commented:
hi zzynx
i've tried your code.but ther entire file is printing.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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.**

Author

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..   .
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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).

Author

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
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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?

Author

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

Author

Commented:
sorry ..my output is the same as my input
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Perhaps there is something wrong with the logic of 'stop' that you're implementing. Can you post the code too, anyway?
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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.

Author

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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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 )
  {
    ....
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Important:

And you do have

 boolean busy = false;

before the while loop?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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?
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Then, how does it come that when performing

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

busy turns to true for e.g. the first line...
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> Can you check in your code
We can keep busy like this...
I posted the whole class tt, let him post it too.

Author

Commented:
it prints the entire file and also prints
 contains end:true  at the end.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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();
    }
}
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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...
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Now that you're online...
What about this question?
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
If mayankeagle agrees, I think I got the right answer.
So my recomm: no split.
What do you think mayankeagle?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Hmmm. You've put me into thought. I wouldn't mind it, zzynxie, but my comment at https://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.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
My third comment was a working (and tested) one.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> I have no doubts that it will not work

I have no doubts that it will work ;-)
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
       Date: 04/07/2004 03:24PM CEST
and
        Date: 04/08/2004 01:13PM CEST
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Thanks.
How do I get the reference (like {http:#10782400} ) to my third comment?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
I see. Never mind.

So,

dear moderator,
the recommandation we both agreed upon is

       Accept: zzynx (third comment on this Q)
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
What would EE-life be easy if all authors closed their Q's...
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.