Solved

startsWith()

Posted on 2004-03-31
36
555 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
  • 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
 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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:
mayankeagle 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:mayankeagle
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:mayankeagle
ID: 10738693
Post your updated code again.
0
 
LVL 30

Expert Comment

by:mayankeagle
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
basic hardware to learn oop advanced design patterns 3 74
mapBully challenge 6 92
pairs challenge 5 45
Problem to start Neon 20 54
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now