We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,608 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Replace method Strings

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?

5
Contributors
6
Replies
7 Hours
Discussion Span
2 Years Ago
Last Updated
7
Views
Question
Answered
theurbanist
Newbie Poster
4 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Prem

prem2
Posting Whiz in Training
228 posts since Feb 2010
Reputation Points: 8
Solved Threads: 4
Skill Endorsements: 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?

masijade
Industrious Poster
Team Colleague
4,253 posts since Feb 2006
Reputation Points: 1,484
Solved Threads: 494
Skill Endorsements: 16

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

JamesCherrill
... trying to help
Moderator
8,497 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29

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! ;-)

masijade
Industrious Poster
Team Colleague
4,253 posts since Feb 2006
Reputation Points: 1,484
Solved Threads: 494
Skill Endorsements: 16

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.

theurbanist
Newbie Poster
4 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Then mark as solved

rapture
Posting Whiz in Training
294 posts since Jul 2007
Reputation Points: 155
Solved Threads: 41
Skill Endorsements: 0
Question Answered as of 2 Years Ago by masijade, rapture, JamesCherrill and 1 other

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0742 seconds using 2.73MB