You are not logged in.
Hi all,
Having some issues in java, decided to take an intro to programming this semester
I want to use the replace method to replace multiple integers with characters,
public class ReplacementTester
{
public static void main(String[] args)
{
String greeting = "H3770, 371t3 hack3r!";
// your work here
// call the replace method four times
System.out.println(modifiedGreeting);
System.out.println("Expected: Hello, elite hacker!");
}
}
I think I need to use
String modifiedGreeting = greeting.replace("7", "l");
in there, which works, but I don't know how to continue it to put more replacements in it. I cant seem to use the same argument again like this, it seems to ignore the second line.
String modifiedGreeting = greeting.replace("7", "l");
String modifiedGreeting = greeting.replace("3", "e");
I know I gotta be missing something relatively simple,
Any ideas?
Last edited by proxima_centauri (2009-01-15 22:33:45)
Thinkpad T61p - 15.4' WSXGA TFT - 2.5Ghz Intel Core2 Duo T9300 - 2X2GB Kingston RAM - 160GB 7200RPM - NVIDIA Quadro FX 570m - Intel 4965AGN
Offline
how about instead of taking the 'greeting' string after the first replacement you actually take the 'modifiedGreeting' string and replace the next chars in that?
With my army of penguins, I shall overthrow governments and free those who have been waiting for liberty.
Offline
You keep recreating the modifiedGreeting reference, so the previous String objects stored their (edit: there) get deleted. I haven't used Java in a long time, but assuming your description of the replace method is correct, the following should work:
public class ReplacementTester {
public static void main(String[] args) {
String greeting = "H3770, 371t3 hack3r!";
String modifiedGreeting = greeting.replace("7", "l");
modifiedGreeting = modifiedGreeting.replace("3", "e");
System.out.println(modifiedGreeting);
System.out.println("Expected: Hello, elite hacker!");
}
}
or to be more concise:
public class ReplacementTester {
public static void main(String[] args) {
String greeting = "H3770, 371t3 hack3r!";
String modifiedGreeting = greeting.replace("7", "l").replace("3", "e");
System.out.println(modifiedGreeting);
System.out.println("Expected: Hello, elite hacker!");
}
}
Last edited by dsr (2009-01-15 21:44:55)
Offline
Looking at it quickly with no java set up on this machine I would say...
public class ReplacementTester
{
public static void main(String[] args)
{
String greeting = "H3770, 371t3 hack3r!";
String modifiedGreeting = greeting.replace("7", "l");
modifiedGreeting = modifiedGreeting.replace("1", "i");
modifiedGreeting = modifiedGreeting.replace("3", "e");
System.out.println(modifiedGreeting);
System.out.println("Expected: Hello, elite hacker!");
}
}
You were redeclaring modifiedGreeting each time thus resetting its value. You also need to run replace on the modified string not the original after the first pass.
Edit:
Too slow. The above solution is more graceful anyway.
Last edited by flowheat (2009-01-15 20:31:27)
Offline
Thanks a lot everyone!
The problem indeed was repeating the subsequent replace lines with modifiedGreeting=greeting.replace when I should have used modifiedGreeting=modifiedGreeting.replace instead.
It makes sense when I think about it. Thanks for the help.
Cheers
Thinkpad T61p - 15.4' WSXGA TFT - 2.5Ghz Intel Core2 Duo T9300 - 2X2GB Kingston RAM - 160GB 7200RPM - NVIDIA Quadro FX 570m - Intel 4965AGN
Offline