File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Replacing all instances of char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Replacing all instances of char" Watch "Replacing all instances of char" New topic
Author

Replacing all instances of char

Zaft kossowski
Greenhorn

Joined: Oct 24, 2012
Posts: 3
I am asked to write a function that inputs a string and two characters, then replaces all instances of the first character with the second character; but I can only use the charAt() and length() methods from the String class. Here is what I have so far. I am hoping someone can help to point me in the right direction.




Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi and welcome to the Ranch!

A couple of things will help you get the most of your experience here.

1. TellTheDetails(←click). If you make it clear exactly what you're having trouble with, people will have a better chance of being able to give you the help you need.

2. When posting code, UseCodeTags(←click) to make it more readable. I've added them to your original post for you.
Zaft kossowski
Greenhorn

Joined: Oct 24, 2012
Posts: 3
Thanks for the comments; I will make sure to follow your suggestions.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

What's line 16 doing in there? I think you should just remove that line.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

I am wondering if you are using replaceAll() method then why are you iterating through the string at the first place.
You can just replaceAll the occurrence simply.


Do not wait to strike till the iron is hot; but make it hot by striking....
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Zaft kossowski wrote:Thanks for the comments; I will make sure to follow your suggestions.

But, as yet, you haven't. What is the problem? Does the method work? If not, what does it return?

However, just a logical note for you. In your original post
you wrote:"I can only use the charAt() and length() methods from the String class"

yet your method contains:
String result=str.replaceAll(""+c1,""+c2);

Surely that violates the rules?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Your original method showed some misunderstanding about how Strings work. Search this website for “Strings, literally” and you get links to an old JavaRanch journal article by that name. Read that carefully. Note especially that Strings are immutable. What you did originally is pull a char out of a String, and replace it. But that has no effect on the original String. You can get the String’s contents as a char[] from one of its methods. You can iterate that array, replacing chars one by one as appropriate. Then you can use the array to create a new String. That may be safer than replaceAll, because you may have problems if you pass a meta‑character for regular expressions as c1 or c2.
There is also a “replace” method for Strings. That might help.

If you have been restricted to length and charAt, you can still use those methods in combination to create and populate a char[].
Zaft kossowski
Greenhorn

Joined: Oct 24, 2012
Posts: 3
@Campbell Ritchie - Thanks for the link. I will make sure to read through the journal.

@Winston Gutkowski - Thank you for pointing out the error of method usage in my posted code; it is the case that I am restricted to only the charAt() and length () methods.

The purpose of the exercise was to write a function that input a String and two characters, and then replaced all occurrences of the first character with the second character, while being restricted to only use of the charAt()/length() methods in the String class. I eventually figured out a solution to the question and realized what I was doing wrong. I was not declaring an equality statement inside the body of the if and else statements; instead, I was declaring the equality statement in the return statement.

See below for solution code, which inputs a String and two characters then replaces all instances of the first character with the second:





I appreciate everyones patience and comments!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
That code has the major problem that you are repeatedly using the + operator in separate lines. It is efficient to use + on Strings many times in one line, but not in several lines, nor in one line called many times.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Campbell Ritchie wrote:That code has the major problem that you are repeatedly using the + operator in separate lines. It is efficient to use + on Strings many times in one line, but not in several lines, nor in one line called many times.

Of course premature optimisation is the root of all evil so you should only worry about this if you can identify this code as the cause of an unacceptable performance bottleneck. Code clarity is much more important - although appending the chars to a StringBuilder (the solution to the problem Campbell pointed out) would not reduce clarity that much.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
An alternative would be to go back to the char[]. Then you can assign all the elements one by one in the loop, with or without replacements, and create only one String.

If you have a 27‑letter String, the performance overhead will be a few μs, so you probably won’t notice it. But in this sort of code, you can sit there watching it chunter away to itself…whereas this sort of thing completes in a fraction of a second:-
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Campbell Ritchie wrote:An alternative would be to go back to the char[]...

@Zaft: And that might be the best of all, since you can easily create a String from a char[] (I presume there are no rules about which String constructors you can use), viz, using your own code as a basis:
Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replacing all instances of char