Replace Method Help
Hi guys,
I'm new to Java and having a bit of trouble with the replace method for my assignment. I'm supposed to get the user to enter a card number and suit and have the program print out that card with certain letters changed. But I'm having difficulties figuring out how I'm supposed to replace the letter "s" with "z" only if the "s" occurs at the very end of the name.
any help is appreciated! thanks
[428 byte] By [
surelykka] at [2007-10-2 10:51:37]

Well the instructions for this part of my assignment is to change the following:
"e" 佀 "3"
"l" 佀 "1" (that is, lowercase letter L replaced with number one)
"a" 佀 "@"
"o" 佀 "0" (that is, lowercase letter O replaced with number zero)
"s" 佀 "z", but only at the end of the word
and so far I have.. .
System.out.print("a card name");
String cardName = scan.nextLine();
System.out.print("a card suit:");
String cardSuit = scan.nextLine();
System.out.println("You picked:");
String cardFinal = cardName + " of " + cardSuit;
cardFinal = cardFinal.replace( 'e', '3' );
cardFinal = cardFinal.replace( 'l', '1' );
cardFinal = cardFinal.replace( 'a', '@' );
cardFinal = cardFinal.replace( 'o', '0' );
System.out.println(cardFinal);
so I've already implemented all the changes except I'm having trouble replacing the "s" with "z" because I'm only supposed to have it replaced if it appears at the end of the word (ie. six of hearts become six of heartz)
any suggestions?
thanks in advance.
oh sorry, the instructions from my top message got a bit messed up but it's supposed to read:
"e" to "3"
"l"to "1" (that is, lowercase letter L replaced with number one)
"a" to "@"
"o" to "0" (that is, lowercase letter O replaced with number zero)
"s" to "z", but only at the end of the word
> so I've already implemented all the changes except
> I'm having trouble replacing the "s" with "z" because
> I'm only supposed to have it replaced if it appears
> at the end of the word (ie. six of hearts become six
> of heartz)
You can't do that with the replace method. You can do it with replaceFirst or replaceAll, but you'll need to learn a bit of regex--in particular that $ means the end of the string.
Or you could do it "manually". If the last character is an s, then create a string that's the same as the original for all but the last character, and then append a z.