We help IT Professionals succeed at work.

find the index of the string

kousis
kousis asked
on
420 Views
Last Modified: 2010-03-31
HEADER    TOPOISOMERASE                           03-FEB-97   1AB4              
TITLE     59KDA FRAGMENT OF GYRASE A FROM E. COLI                              
COMPND    MOL_ID: 1;                                                            
COMPND   2 MOLECULE: GYRASE A;                                                  
COMPND   3 CHAIN: NULL;                                                        
COMPND   4 FRAGMENT: 59KDA FRAGMENT;                                            
COMPND   5 EC: 5.99.1.3;                                                        
COMPND   6 ENGINEERED: YES;                                                    
COMPND   7 BIOLOGICAL_UNIT: DIMER                                              
SOURCE    MOL_ID: 1;                                                            
SOURCE   2 ORGANISM_SCIENTIFIC: ESCHERICHIA COLI;                              
SOURCE   3 EXPRESSION_SYSTEM: ESCHERICHIA COLI                                  
KEYWDS    TOPOISOMERASE II, GYRASE, SUPERCOILING DNA                            
EXPDTA    X-RAY DIFFRACTION                                                    
AUTHOR    J.H.M.CABRAL,A.MAXWELL,R.C.LIDDINGTON                                
REVDAT   1   14-OCT-98 1AB4    0                                                
JRNL        AUTH   J.H.CABRAL,A.P.JACKSON,C.V.SMITH,N.SHIKOTRA,                
JRNL        AUTH 2 A.MAXWELL,R.C.LIDDINGTON                                    
JRNL        TITL   CRYSTAL STRUCTURE OF THE BREAKAGE-REUNION DOMAIN OF          
JRNL        TITL 2 DNA GYRASE                                                  
ATOM      1  N   VAL    30      43.915  76.424  29.465  1.00100.00           N  
ATOM      2  CA  VAL    30      43.454  77.777  29.905  1.00100.00           C  
ATOM      3  C   VAL    30      44.406  78.396  30.932  1.00100.00           C  
ATOM      4  O   VAL    30      44.773  79.571  30.829  1.00100.00           O  
ATOM      5  CB  VAL    30      42.003  77.729  30.486  1.00100.00           C  
ATOM      6  CG1 VAL    30      40.998  77.426  29.379  1.00 85.18           C  
ATOM      7  CG2 VAL    30      41.890  76.676  31.589  1.00 85.18           C  

The above mentioned is my file. I want to print form 'header' till "GYRASE" and save it another file.
how to find the index?
TIA
Comment
Watch Question


int end=str.indexOf("GYRASE");
System.out.println(str.substring(0,end));

Author

Commented:
int end=str.indexOf("GYRASE");
System.out.println(str.substring(0,end));
I've already used this command.
 it's giving me error-
Stringindexoutof boundsException
how to rectify it?
Top Expert 2004

Commented:
int end=str.indexOf("GYRASE");
if (end<0) System.out.println("no GYRASE");
else System.out.println(str.substring(0,end));
Top Expert 2004

Commented:
You may need to seek "ATOM " in fact :

int end=str.indexOf("ATOM ");
if (end<0) System.out.println(str);
else System.out.println(str.substring(0,end)); // ATOM not included
Top Expert 2004

Commented:
If "ATOM " can appear in the middle of a header line :
int end=str.indexOf("\nATOM");
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>>  it's giving me error- Stringindexoutof boundsException
If the string ("GYRASE") is not found then indexOf() returns -1
Using -1 in substring() gives the error.

>> how to rectify it?
Check for -1 value like Webstorm said in

>>int end=str.indexOf("GYRASE");
>>if (end<0) System.out.println("no GYRASE");
>>else System.out.println(str.substring(0,end));
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> it's giving me error-
>> Stringindexoutof boundsException

Its thrown when you try to access a String outside its limits. You must be getting StringIndexOutOfBoundsException: -1

That is because 'end' would be -1, perhaps because:

>> int end=str.indexOf("GYRASE");

- I guess "GYRASE" is not found in the String. The exception can also be thrown if the String is of 0 length, because then str.substring (0, anything) will also be invalid.... since str.charAt ( 0 ) is also not defined.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Aw.... sorry about the -1 thing, zzynx, didn't refresh.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
No problem mayankeagle
Refreshing seems to be a common problem for us all
;)
Top Expert 2004

Commented:
>> The exception can also be thrown if the String is of 0 length, because then str.substring (0, anything) will also be invalid....
except for anything==0 because substring(start,end) end is not included (length=end-start)

Author

Commented:
ATOM exists in my file, eventhough it's returning -1. I don't know why? is any other way to get the index?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Post your code for reading the file.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> ATOM exists in my file, eventhough it's returning -1
Impossible. There must be something wrong.
Can you post the code where you process your file (line by line I suppose)

>> is any other way to get the index?
I would search for the reason "why?" instead of using another method
(that will give the same result)
It's always good to understand why things happen.

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Are you reading it line by line (I guess so)?

In that case, every line (String) that you read will not contain "ATOM" or whatever String you're searching for.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
:-)
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
We're multiprocessing
;)

Author

Commented:
yes, I am reading it line by line I'll try to change my code.

Author

Commented:
import java.io.*;
import java.lang.*;
import java.lang.String;
import java.util.*;
import java.text.*;
public class Copy3 {
    public static void main(String[] args) throws IOException {
       File inputFile = new File("pro.rtf");
                        FileReader in = new FileReader(inputFile);
      String s;
                         BufferedReader br=new BufferedReader(in);
      while((s=br.read())!=null)
      {
      int end=s.indexOf("ATOM");
      if(end<0)
      System.out.println("No atom");
      else
      System.out.println(s.substring(0,end));
      }
      }
      }
This is my code.

Author

Commented:
sorry that was br.readLine()!=Null
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Use:

boolean flag = false ;

while((s=br.read())!=null)
      {
      int end=s.indexOf("ATOM");
      if ( end >= 0 )
      {
        flag = true ;
        System.out.println(s.substring(0,end));
      }
}

if ( flag == false )
  System.out.println ( "No ATOM" ) ;

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
What you're doing is - you're reading each line. If the line does not contain "ATOM", you're printing "No ATOM". That is wrong. Every line need not contain "ATOM". You have to read till you get "ATOM" and if you don't get "ATOM" till the end of the file, you should declare that "ATOM" was not found. That's why I've used a Boolean flag.

I guess you could add a break statement to the if ( end >= 0 ) block in my code.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
I'm wondering why we talk about "ATOM" while your initial question was about "GYRASE"?
kousis, could you lead us to the right answering path again?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Whatever it is, just the String in the indexOf () call needs to be changed. Perhaps there was a question from the same person before this one which had some similar thing to do with "ATOM".... maybe that's why Webstorm suggested it (and indeed, the questioner too started asking about "ATOM" only, instead of "GYRASE").
Top Expert 2004

Commented:

Try this:

      while((s=br.read())!=null)
      {
          if (s.startsWith("ATOM")) break;
          System.out.println(s);
      }
Top Expert 2004

Commented:
The code i posted before (using indexOf) was good if you were reading the whole file in one big String.

Author

Commented:
I got to print the text from the "HEADER" till the first word "ATOM"
import java.lang.String;
import java.util.*;
import java.text.*;
public class Copy3 {
    public static void main(String[] args) throws IOException {
       File inputFile = new File("pro.rtf");
                        FileReader in = new FileReader(inputFile);
      String s;
              int i;
boolean flag=false;
           BufferedReader br=new BufferedReader(in);
      while((i=br.read())!=0)
      {
s=Integer.toString(i);

int end=s.indexOf("ATOM");
      if ( end >= 0 )
      {
        flag = true ;
        System.out.println(s.substring(0,end));
      }

}  
if ( flag == false )
  System.out.println ( "No ATOM" ) ;


 
      }
      }
I modified the program with this code. but it's hanging.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>>Try this:
>>
>>      while((s=br.read())!=null)
>>      {
>>          if (s.startsWith("ATOM")) break;
>>          System.out.println(s);
>>      }

Oh, I see, you want to print out all header lines till you reach your first ATOM line.
It's always good to explain as much as possible...
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Webstorm talked about startsWith("ATOM") instead of indexOf("ATOM")
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> while((i=br.read())!=0)

Read a line, not an int.

while ( ( String strLine = br.readLine () ) != null )
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Why do you do this

s=Integer.toString(i);
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> s=Integer.toString(i);

It will give you String equivalent of the integer you read. Does you no good. You are not reading an entire line. Instead of "ATOM .... (followed by the whole line)", you are reading ints one by one. Does you no good.

Try:

while ( ( s = br.readLine () ) != null )
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
while  ( s = br.readLine () ) != null )
      {
      int end=s.indexOf("ATOM");
      if ( end >= 0 )
      {
        flag = true ;
        System.out.println(s.substring(0,end));
      }
      else
        System.out.println ( s ) ;

}  
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
import java.lang.String;
import java.util.*;
import java.text.*;
public class Copy3 {
    public static void main(String[] args) throws IOException {
       File inputFile = new File("pro.rtf");
                        FileReader in = new FileReader(inputFile);
      String s;
      boolean flag=false;
      BufferedReader br=new BufferedReader(in);

      while((s=br.readLine())!=null)
      {
            int end=s.indexOf("ATOM");
            if ( end >= 0 )
            {
               flag = true ;
               System.out.println(s.substring(0,end));
            }
     }  

     if ( flag == false )
        System.out.println ( "No ATOM" ) ;
 
     }
}
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> startsWith("ATOM")

What if there is a blank space before the "ATOM"? Then you will have to trim () it. I guess indexOf () would be better for all cases.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Multiprocessing continues ;-)

>> while  ( s = br.readLine () ) != null )

Missed an extra ( there. Refer to my previous comment, before that one.

while ( ( s = br.readLine () ) != null )

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Just noticed that you are reading an RTF file. RTF files won't get read directly using readLine (). You need to use different API for that. Is it that you have only plain text in that file, and you gave it an extension RTF just like that, or have you created the file in WordPad or Word?
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Ignore my previous comment. This is it:

import java.lang.String;
import java.util.*;
import java.text.*;
public class Copy3 {
    public static void main(String[] args) throws IOException {
       File inputFile = new File("pro.rtf");
                        FileReader in = new FileReader(inputFile);
      String s;
      boolean flag=false;
      BufferedReader br=new BufferedReader(in);

      while((s=br.readLine())!=null)
      {
            boolean stop = s.startsWith("ATOM");
            if ( !stop )
               System.out.println(s);
      }
}
}
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> Multiprocessing continues ;-)
kousis blown away?
;-)
Principal Technologist
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:
Better not continue reading when not needed so added an "else break":

      while((s=br.readLine())!=null)
      {
            boolean stop = s.startsWith("ATOM");
            if ( !stop )
               System.out.println(s);
            else
               break;
      }
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> // I GUESS THAT IS WHAT HE WANTS TO DO - not sure          
>> System.out.println(s.substring ( 0, s.indexOf ("ATOM" ) );

Now again.... why to call startsWith () and also call indexOf ().... becomes an extra method call. So I would prefer:

while((s=br.readLine())!=null)
      {
            end = s.indexOf ("ATOM");
            if ( end < 0 )
               System.out.println(s);
            else
            {
               stop = true ;
               System.out.println(s.substring ( 0, end ));
               break ;
            }
      }

Also rules out the case when there is a blank-space at the start. In the previous code:

>> System.out.println(s.substring ( 0, s.indexOf ("ATOM" ) );

Missed an extra ) at the end.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
I think we're driving kousis mad, don't we?
LOL :)
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> so added an "else break":

Yeah, already done ;-)

Anyway, kousis is perhaps offline.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> I think we're driving kousis mad, don't we?

Perhaps. We might freeze posting comments for a while, till he returns with feedback ;-)
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> startsWith () and also call indexOf ()....
Where do you find *both* in my comment?

But, as for the starting with a space you're right.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> Where do you find *both* in my comment?

I was not talking about your comment ;-) I was optimizing my previous comment.
Top Expert 2004

Commented:


      while((s=br.readLine())!=null)
      {
            if (s.trim().startsWith("ATOM")) break;
            // or  if (s.indexOf("ATOM ")>=0) break; // but it will not work for JRNL   TITL 2 DNA NATOM for example
            System.out.println(s);
      }
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> // but it will not work for JRNL   TITL 2 DNA NATOM for example

Why? It will yield a true. I guess that is the kind of situation which the questioner was addressing when he was using:

>> System.out.println(s.substring(0,end));

I guess he wanted to that.
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.