Solved

print page from desired position

Posted on 2004-04-07
41
215 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
0
Comment
Question by:kousis
  • 21
  • 11
  • 7
41 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10774337
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10774413
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
 
LVL 37

Accepted Solution

by:
zzynx earned 100 total points
ID: 10774501
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10774521
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
 

Author Comment

by:kousis
ID: 10780851
hi zzynx
i've tried your code.but ther entire file is printing.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10781235
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
 

Author Comment

by:kousis
ID: 10781485
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10781533
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10781661
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
 

Author Comment

by:kousis
ID: 10781764

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
 
LVL 37

Expert Comment

by:zzynx
ID: 10781812
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
 

Author Comment

by:kousis
ID: 10782004
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
 

Author Comment

by:kousis
ID: 10782030
sorry ..my output is the same as my input
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10782039
Perhaps there is something wrong with the logic of 'stop' that you're implementing. Can you post the code too, anyway?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10782051
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
 

Author Comment

by:kousis
ID: 10782094
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10782111
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10782143
Important:

And you do have

 boolean busy = false;

before the while loop?
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10782150
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 37

Expert Comment

by:zzynx
ID: 10782158
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
 
LVL 37

Expert Comment

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

Author Comment

by:kousis
ID: 10782180
it prints the entire file and also prints
 contains end:true  at the end.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10782194
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10782400
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10787332
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10789327
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10831117
Now that you're online...
What about this question?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11047457
If mayankeagle agrees, I think I got the right answer.
So my recomm: no split.
What do you think mayankeagle?
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11047583
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11047623
My third comment was a working (and tested) one.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11047655
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11047665
>> I have no doubts that it will not work

I have no doubts that it will work ;-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11047697
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11047705
       Date: 04/07/2004 03:24PM CEST
and
        Date: 04/08/2004 01:13PM CEST
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11047735
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11047744
Thanks.
How do I get the reference (like {http:#10782400} ) to my third comment?
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11047783
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11047835
I see. Never mind.

So,

dear moderator,
the recommandation we both agreed upon is

       Accept: zzynx (third comment on this Q)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11047839
What would EE-life be easy if all authors closed their Q's...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now