• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

find the index of the string

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
0
kousis
Asked:
kousis
  • 20
  • 14
  • 7
  • +2
1 Solution
 
venkateshwarrCommented:

int end=str.indexOf("GYRASE");
System.out.println(str.substring(0,end));
0
 
kousisAuthor 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?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
WebstormCommented:
int end=str.indexOf("GYRASE");
if (end<0) System.out.println("no GYRASE");
else System.out.println(str.substring(0,end));
0
 
WebstormCommented:
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
0
 
WebstormCommented:
If "ATOM " can appear in the middle of a header line :
int end=str.indexOf("\nATOM");
0
 
zzynxSoftware engineerCommented:
>>  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));
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Aw.... sorry about the -1 thing, zzynx, didn't refresh.
0
 
zzynxSoftware engineerCommented:
No problem mayankeagle
Refreshing seems to be a common problem for us all
;)
0
 
WebstormCommented:
>> 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)

0
 
kousisAuthor Commented:
ATOM exists in my file, eventhough it's returning -1. I don't know why? is any other way to get the index?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Post your code for reading the file.
0
 
zzynxSoftware engineerCommented:
>> 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.

0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
:-)
0
 
zzynxSoftware engineerCommented:
We're multiprocessing
;)
0
 
kousisAuthor Commented:
yes, I am reading it line by line I'll try to change my code.
0
 
kousisAuthor 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.
0
 
kousisAuthor Commented:
sorry that was br.readLine()!=Null
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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" ) ;

0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
zzynxSoftware engineerCommented:
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?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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").
0
 
WebstormCommented:

Try this:

      while((s=br.read())!=null)
      {
          if (s.startsWith("ATOM")) break;
          System.out.println(s);
      }
0
 
WebstormCommented:
The code i posted before (using indexOf) was good if you were reading the whole file in one big String.
0
 
kousisAuthor 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.
0
 
zzynxSoftware engineerCommented:
>>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...
0
 
zzynxSoftware engineerCommented:
Webstorm talked about startsWith("ATOM") instead of indexOf("ATOM")
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> while((i=br.read())!=0)

Read a line, not an int.

while ( ( String strLine = br.readLine () ) != null )
0
 
zzynxSoftware engineerCommented:
Why do you do this

s=Integer.toString(i);
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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 )
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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 ) ;

}  
0
 
zzynxSoftware engineerCommented:
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" ) ;
 
     }
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Multiprocessing continues ;-)

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

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

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

0
 
Mayank SAssociate Director - Product EngineeringCommented:
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?
0
 
zzynxSoftware engineerCommented:
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);
      }
}
}
0
 
zzynxSoftware engineerCommented:
>> Multiprocessing continues ;-)
kousis blown away?
;-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> boolean stop = s.startsWith("ATOM");

But there again comes the issue. What if there is a blank space or something like that at the start?

Assuming it is a normal file, I would modify it slightly:

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 stop=false;
      BufferedReader br=new BufferedReader(in);

      while((s=br.readLine())!=null)
      {
            stop = s.startsWith("ATOM");
            if ( !stop )
               System.out.println(s);
            else
            {
               // I GUESS THAT IS WHAT HE WANTS TO DO - not sure          
               System.out.println(s.substring ( 0, s.indexOf ("ATOM" ) );
               break ;
            }
      }

if ( ! stop )
  System.out.println ( "ATOM NOT FOUND" ) ;
}

}
0
 
zzynxSoftware engineerCommented:
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;
      }
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> // 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.
0
 
zzynxSoftware engineerCommented:
I think we're driving kousis mad, don't we?
LOL :)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> so added an "else break":

Yeah, already done ;-)

Anyway, kousis is perhaps offline.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I think we're driving kousis mad, don't we?

Perhaps. We might freeze posting comments for a while, till he returns with feedback ;-)
0
 
zzynxSoftware engineerCommented:
>> startsWith () and also call indexOf ()....
Where do you find *both* in my comment?

But, as for the starting with a space you're right.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Where do you find *both* in my comment?

I was not talking about your comment ;-) I was optimizing my previous comment.
0
 
WebstormCommented:


      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);
      }
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> // 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 20
  • 14
  • 7
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now