1,105,534 Community Members

Replace method Strings

Member Avatar
theurbanist
Newbie Poster
4 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm trying to write a program that switches the "x" and "o" in a String, only using the replace method repeatedly.

At the end of the day, the program should turn 'xxxooo' into 'oooxxx'.

I understand why this isn't working, but I'm just completely out of ideas as to how to make it work. So here's what I have:

firstStep = console.nextLine();
secondStep = firstStep.replaceAll(o, x);
thirdStep = thirdStep.replace(x, o);
System.out.println(thirdStep);

Is there a way to accomplish this replacement in one step?

Member Avatar
prem2
Posting Whiz in Training
233 posts since Feb 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

kindly display your program so that it will me more clear to understand.

Prem

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
1
 

Actually, that won't work, anyway. As if you replace all o with x then all x with o your string will consist of nothing but o

Step 1: get string
Step 2: replace x with z
Step 3: replace o with x
Step 4: replace z with o

Why do you feel the need to do it in one?

Member Avatar
JamesCherrill
... trying to help
10,411 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,753 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Even at the pseudocode level that's not going to work:
firstStep = console.nextLine(); eg "ooxxx"
secondStep = firstStep.replaceAll(o, x); "xxxxx"
thirdStep = thirdStep.replace(x, o); "ooooo"
System.out.println(thirdStep); "ooooo"

This needs an extra step:
replace all the o with some temp char
replace all the x with o
replace all the temp char with x

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

Even at the pseudocode level that's not going to work:
firstStep = console.nextLine(); eg "ooxxx"
secondStep = firstStep.replaceAll(o, x); "xxxxx"
thirdStep = thirdStep.replace(x, o); "ooooo"
System.out.println(thirdStep); "ooooo"

This needs an extra step:
replace all the o with some temp char
replace all the x with o
replace all the temp char with x

Too slow! ;-)

Member Avatar
theurbanist
Newbie Poster
4 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Actually, that won't work, anyway. As if you replace all o with x then all x with o your string will consist of nothing but o

Step 1: get string
Step 2: replace x with z
Step 3: replace o with x
Step 4: replace z with o

Why do you feel the need to do it in one?

Thank you so much, when I woke up this morning my brain had also determined this path and I got it working! I appreciate your help.

Member Avatar
rapture
Posting Whiz in Training
294 posts since Jul 2007
Reputation Points: 134 [?]
Q&As Helped to Solve: 41 [?]
Skill Endorsements: 0 [?]
 
0
 

Then mark as solved

Question Answered as of 3 Years Ago by masijade, rapture, JamesCherrill and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: