We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

startsWith()

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

zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
code please
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?
}

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

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

Commented:
Hi mayank, join the club.
Funny, 4 experts guessing what the author probably means...
;)
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Hi zzynxie :-) Hey, did luna give you those points? Do you want me to post on that Q again to remind her/ him?
zzynxSr. Software engineer
CERTIFIED EXPERT

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

Commented:
All right, as you wish.

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

Author

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

Commented:
your variable n doesn't change
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
So in every loop you add 'A' at the same place
Top Expert 2004

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

Top Expert 2004

Commented:

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)

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

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

Author

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

Author

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

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

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

Commented:
By the way - what do you have the BufferedReader and the BufferedWriter for? You don't seem to be using them anywhere.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
You could use:

bw.write ( s ) ;
bw.newLine () ;

Author

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

Author

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

Commented:
What is fw? Is it a FileWriter like out?

Did you try:

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

?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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 () ;

Author

Commented:
yes,fw is filewriter
bw.newLine(); is also not working
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Post your updated code (full).

Don't use bw and fw together (I mean - fw.write () and bw.newLine ()). Use only bw.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Put bw.newLine () in the loop with bw.write ().

Author

Commented:
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
Principal Technologist
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
as venabili mentioned. i printed the file. i got all the strings with additional junk values. what will be the wrong
Mayank SPrincipal Technologist
CERTIFIED EXPERT

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

Author

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

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

Commented:
Post your updated code again.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

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