Link to home
Start Free TrialLog in
Avatar of kousis
kousis

asked on

startsWith()

hi
I want to insert a character in the string whisch starts with ATOM. actually the sequence is stored in the file.
I used startsWith("ATOM") but it's adding to the character to first string but not to the remaining strings. how it can be rectified?
TIA
Avatar of zzynx
zzynx
Flag of Belgium image

code please
Avatar of tomboshell
tomboshell

Should be in your loop somewhere.  I would assume that it is a loop reading the file, with each iteration you are reading a line and checking this condition, when true you want to add to the string.  So then the question is if you are reading all the lines, and then what are you doing after that (rewriting the file, or sending it to somewhere in the program), just make sure that you are not breaking your loop.   Probably something like...
String s = new String();
while( (s = br.readLine())!=null){  // br is a BufferedReader
    if(s.startsWith("ATOM")){
        StringBuffer buf = new StringBuffer(s);
        buf.insert(0, "something");
        s = buf.toString();
    }
    // then what to do with the string?
}
If your file has file separators and some lines have ATOM string then this code

String s = null;
while((s = bufferedReader.readLine()) != null) {
    if( s.trim().startsWith("ATOM") ) {
        // do insert a char here
    }
}

should works.
>> but it's adding to the character to first string but not to the remaining strings

You either don't have a loop, or you have a break ; statement inside the loop which brings it out after the first String matched (I think you have a break ; - that's what I remember from your previous question which was about how to find a String in a file that starts with "ATOM") - try removing the break ; and see what happens.
Hi mayank, join the club.
Funny, 4 experts guessing what the author probably means...
;)
Hi zzynxie :-) Hey, did luna give you those points? Do you want me to post on that Q again to remind her/ him?
Not yet. But I guess (s)he not online for the moment.

>> Do you want me to post on that Q again to remind her/ him?
Kind of you.
But no, I posted a rather "clear" comment and I guess luna will correct that soon.
All right, as you wish.

kousis, any feedback on what we've told you?
Avatar of kousis

ASKER

hi
sorry i was not online for some time
my code is this....

import java.io.*;
import java.lang.*;
import java.lang.String;
import java.util.*;
import java.text.*;
public class trans1
{
public static void main(String args[])throws IOException
{
File inputfile=new File("ptn.rtf");
File outputfile=new File("ptncopy.rtf");

FileReader in= new FileReader(inputfile);
FileWriter out=new FileWriter(outputfile);
BufferedReader br=new BufferedReader(in);
BufferedWriter bw=new BufferedWriter(out);
String s;
int n=0;

char a='A';

while((s=br.readLine())!=null)
{
if(s.startsWith("ATOM"))
{
s=s.substring(0,n+21)+a+s.substring(n+22);
}
out.write(s);

}
in.close();
out.close();
}
}
your variable n doesn't change
So in every loop you add 'A' at the same place
>> s=s.substring(0,n+21)+a+s.substring(n+22);
You may get an exception if s.length() < n+21

If you want to add a 'A' instead of replacing the (n+20)th character :

s=s.substring(0,n+21)+a+s.substring(n+21);


if (s.length()>=(n+21))
    s=s.substring(0,n+21)+a+s.substring(n+21);
    //  substring(0,n+21) : from 0 (included), to n+21 (not included)

>>used startsWith("ATOM") but it's adding to the character to first string but not to the remaining strings. how it can be rectified?

Are you sure all  your strings starts with it? No leading spaces and so on?
Exactly, you should trim () the String.

But more - I see that you are reading an RTF  file. You cannot read an RTF file directly as text just by the readLine () method as far as I know, unless your RTF file contains only text and is given a .rtf extension just like that.

By the way, what is the significance of 21, 22??
Avatar of kousis

ASKER

my rtf file contains only text.My file contains txt like the following.
ATOM    186  O   VAL   32      15.422  28.707  35.170  1.00 25.69      1APN 394
ATOM    187  CB  VAL   32      18.407  27.830  35.063  1.00 24.68      1APN 395

I got to maintain the spacing.that's very important.There is no lading spacs before thee word ATOM.
Avatar of kousis

ASKER

my rtf file contains only text.My file contains txt like the following.
ATOM    186  O   VAL   32      15.422  28.707  35.170  1.00 25.69      1APN 394
ATOM    187  CB  VAL   32      18.407  27.830  35.063  1.00 24.68      1APN 395

I got to maintain the spacing.that's very important.There is no leading spacs before thee word ATOM.
Try to print the string to see if it is ok

while((s=br.readLine())!=null){
  System.out.println(s);
  if(s.startsWith("ATOM")){
    s=s.substring(0,n+21)+a+s.substring(n+22);
  }
  out.write(s);
}

Is this showing all the strings?
I compiled and ran the code, and it DID add "A" to each new line in ptncopy.rtf, but it DID NOT put a newline at the end of each line. I made this change:

s=s.substring(0,n+21)+a+s.substring(n+22)+"\n";

And got this result:

ATOM    186  O   VAL A 32      15.422  28.707  35.170  1.00 25.69      1APN 394
ATOM    187  CB  VAL A 32      18.407  27.830  35.063  1.00 24.68      1APN 395

Is that NOT what you are looking for? It seems from your description that it is.....
By the way - what do you have the BufferedReader and the BufferedWriter for? You don't seem to be using them anywhere.
You could use:

bw.write ( s ) ;
bw.newLine () ;
Avatar of kousis

ASKER

while((str=br.readLine())!=null)
{
if(str.startsWith("ATOM"))
 
str=str.substring(0,n+21)+c+str.substring(n+21)"\n";

fw.write(str);

}
I used the above code.,but it's not inserting the char in the next line. it remains the same.is there any method to rectify. or am i using the loop in the right way?
pls rectify it.
TIA
Avatar of kousis

ASKER

while((str=br.readLine())!=null)
{
if(str.startsWith("ATOM"))
 
str=str.substring(0,n+21)+c+str.substring(n+21)+"\n";

fw.write(str);

}
I used the above code.,but it's not inserting the char in the next line. it remains the same.is there any method to rectify. or am i using the loop in the right way?
pls rectify it.
TIA
What is fw? Is it a FileWriter like out?

Did you try:

bw.write ( str ) ;
bw.newLine () ;

?
>> bw.newLine () ;

In that case, don't put + "\n" in your String (s or str - whatever it is).

In the end of the program, write:

bw.flush () ;
Avatar of kousis

ASKER

yes,fw is filewriter
bw.newLine(); is also not working
Post your updated code (full).

Don't use bw and fw together (I mean - fw.write () and bw.newLine ()). Use only bw.
Put bw.newLine () in the loop with bw.write ().
Avatar of kousis

ASKER

mport java.io.*;
import java.lang.*;
import java.util.*;
public class trans2
{
public static void main(String args[])throws IOException
{
File fi=new File("prt.rtf");
File fo=new File("prtcopy.rtf");
FileReader fr=new FileReader(fi);
FileWriter fw=new FileWriter(fo);
BufferedReader br=new BufferedReader(fr);
BufferedWriter bw=new BufferedWriter(fw);
String str;
int n=0;
char c='A';
while((str=br.readLine())!=null)
{
if(str.startsWith("ATOM"))
{
str=str.substring(0,n+21)+c+str.substring(n+22);
}
fw.write(str);
bw.newLine();
}
fr.close();
fw.close();
}
}

This is my updated code
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India 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
Avatar of kousis

ASKER

as venabili mentioned. i printed the file. i got all the strings with additional junk values. what will be the wrong
You are mixing fw and bw, like I told you 2 times - you should not do it. Did you change fw.write ( str ) to bw.write ( str ) ; ?
Avatar of kousis

ASKER

yes , i changed it. now i am getting the out put with the character printed except the First ATOM string. the following is the out put
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;                                                        ATOM      2  CA  VAL    30      43.454  77.777  29.905  1.00100.00           C  ATOM      3  C   VAL A  30      44.406  78.396  30.932  1.00100.00           C  ATOM      4  O   VAL A  30      44.773  79.571  30.829  1.00100.00           O  ATOM      2  CA  VAL A  30      43.454  77.777  29.905  1.00100.00           C  END
Your first posted code (trans1 class) and eicheled's comment can solve your problem on my computer.

>I used the above code.,but it's not inserting the char in the next line. it remains the same
Make sure you did check it on the right file (i mean .. not old file).
Post your updated code again.
BTW, why a B :-(

A B-grade is given if the answer was kind of incomplete and you had to do some hunting on your own. Not if the question is easy or expert posted something which helped you completely.

https://www.experts-exchange.com/help/closing.jsp