Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replacing all instances of char

 
Zaft kossowski
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the comments; I will make sure to follow your suggestions.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's line 16 doing in there? I think you should just remove that line.
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic