Solved

startsWith()

Posted on 2004-03-31
36
563 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
0
Comment
Question by:kousis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 9
  • 5
  • +5
36 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10730168
code please
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 10730237
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?
}
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10730245
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.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 30

Expert Comment

by:Mayank S
ID: 10730280
>> 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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10730294
Hi mayank, join the club.
Funny, 4 experts guessing what the author probably means...
;)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10730324
Hi zzynxie :-) Hey, did luna give you those points? Do you want me to post on that Q again to remind her/ him?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10730335
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10730349
All right, as you wish.

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

Author Comment

by:kousis
ID: 10731489
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();
}
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10731552
your variable n doesn't change
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10731561
So in every loop you add 'A' at the same place
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10731598
>> 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);

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10731613

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)

0
 
LVL 20

Expert Comment

by:Venabili
ID: 10731937
>>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?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10732070
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??
0
 

Author Comment

by:kousis
ID: 10732672
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.
0
 

Author Comment

by:kousis
ID: 10732674
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.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 10732928
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?
0
 
LVL 3

Expert Comment

by:eicheled
ID: 10733937
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.....
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738307
By the way - what do you have the BufferedReader and the BufferedWriter for? You don't seem to be using them anywhere.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738329
You could use:

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

Author Comment

by:kousis
ID: 10738417
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
0
 

Author Comment

by:kousis
ID: 10738418
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738427
What is fw? Is it a FileWriter like out?

Did you try:

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

?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738429
>> 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 () ;
0
 

Author Comment

by:kousis
ID: 10738445
yes,fw is filewriter
bw.newLine(); is also not working
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738465
Post your updated code (full).

Don't use bw and fw together (I mean - fw.write () and bw.newLine ()). Use only bw.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738472
Put bw.newLine () in the loop with bw.write ().
0
 

Author Comment

by:kousis
ID: 10738512
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
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 100 total points
ID: 10738518
Like I said already - don't mix fw and bw.

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

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

Before closing:

>> fr.close();
>> fw.close();

bw.flush () ;
0
 

Author Comment

by:kousis
ID: 10738538
as venabili mentioned. i printed the file. i got all the strings with additional junk values. what will be the wrong
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738553
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 ) ; ?
0
 

Author Comment

by:kousis
ID: 10738677
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
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10738680
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).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738693
Post your updated code again.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10739097
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.

http://www.experts-exchange.com/help/closing.jsp
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question