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
Java

Avatar of undefined
Last Comment
Mayank S

8/22/2022 - Mon
zzynx

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

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Mayank S

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

Hi mayank, join the club.
Funny, 4 experts guessing what the author probably means...
;)
Mayank S

Hi zzynxie :-) Hey, did luna give you those points? Do you want me to post on that Q again to remind her/ him?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
zzynx

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 S

All right, as you wish.

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

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 help has saved me hundreds of hours of internet surfing.
fblack61
zzynx

your variable n doesn't change
zzynx

So in every loop you add 'A' at the same place
Webstorm

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Webstorm


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)

Venabili

>>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 S

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??
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
kousis

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.
ASKER
kousis

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

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
eicheled

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 S

By the way - what do you have the BufferedReader and the BufferedWriter for? You don't seem to be using them anywhere.
Mayank S

You could use:

bw.write ( s ) ;
bw.newLine () ;
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
kousis

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
ASKER
kousis

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 S

What is fw? Is it a FileWriter like out?

Did you try:

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

?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mayank S

>> 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 () ;
ASKER
kousis

yes,fw is filewriter
bw.newLine(); is also not working
Mayank S

Post your updated code (full).

Don't use bw and fw together (I mean - fw.write () and bw.newLine ()). Use only bw.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Mayank S

Put bw.newLine () in the loop with bw.write ().
ASKER
kousis

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
Mayank S

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
kousis

as venabili mentioned. i printed the file. i got all the strings with additional junk values. what will be the wrong
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mayank S

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 ) ; ?
ASKER
kousis

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
gnoon

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).
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Mayank S

Post your updated code again.
Mayank S

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