You are not logged in.

#1 2009-01-15 19:50:45

proxima_centauri
Member
From: Nova Scotia, Canada
Registered: 2008-07-17
Posts: 117
Website

Beginner Java: Replace method <solved>

Hi all,

Having some issues in java, decided to take an intro to programming this semester big_smile

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

#2 2009-01-15 20:26:16

zandaa
Member
From: The Netherlands
Registered: 2008-07-14
Posts: 33
Website

Re: Beginner Java: Replace method <solved>

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

#3 2009-01-15 20:27:55

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: Beginner Java: Replace method <solved>

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

#4 2009-01-15 20:29:54

flowheat
Member
From: Pittsburgh, PA
Registered: 2008-09-23
Posts: 94

Re: Beginner Java: Replace method <solved>

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

#5 2009-01-15 22:33:19

proxima_centauri
Member
From: Nova Scotia, Canada
Registered: 2008-07-17
Posts: 117
Website

Re: Beginner Java: Replace method <solved>

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

Board footer

Powered by FluxBB