Free Scan: Update Your PC's Outdated Drivers to Optimize Performance
September 21st, 2008, 03:50 PM
|
#1 (permalink)
| | Junior Member
Join Date: Sep 2008
Posts: 2
| Java : Would like help in solving "programming exercises"
I would appreciate it if someone could help me. I've been given specific directions to write a "simple" program in Java, but I can't seem to get the results i desire.
Problem:
Write a program that encodes a string by replacing all letters "i" with "!" and all letters "s" with "$". Use the replace method. Demonstrate that you can correctly encode the string "Mississippi".
So I need to change "Mississippi" into "M!$$!$$!pp!", here's what I have to far: Code: import java.lang.String;
public class Mississippi
{
public static void main (String[] args)
{
String msp = "Mississippi";
System.out.print (msp.replace ("i" , "!"));
}
} Everything else I've tried wouldn't compile or didn't give me the solution I need, such as: Code: ...
msp.replace ("i", "!");
System.out.print (msp);
Gives me "Mississippi"
or
System.out.print (msp.replace ("i" , "!") msp.replace ("s" , "$"));
I get a syntax error when compiling
or
System.out.print (msp.replace ("i" , "!");
System.out.print (msp.replace ("s" , "$");
Gives me "M!ss!ss!pp!Mi$$i$$ippi" |
| |
September 21st, 2008, 03:58 PM
|
#2 (permalink)
| | Go back to sleep
Join Date: Jul 2002 Location: Switzerland
Posts: 6,052
|
well the best way to actually do this is to break the string apart and read every single letter at once and check if it's an s or a i and switch the letter if necessary
of course it uses more code but should work a lot better, i could give it a try, but my last java job was a game engine which had nothing to do with strings but if you want me to, i could wrap something up
Creatures
__________________
Knowledge Is Power
|
| |
September 21st, 2008, 05:21 PM
|
#3 (permalink)
| | Go back to sleep
Join Date: Jul 2002 Location: Switzerland
Posts: 6,052
|
well replace should work ;P Code: {
String str = "Mississippi"; //creating a string str containing "Mississippi"
str = str.replace('i', '!'); //replacing i's with !'s and storing string in str
str = str.replace('s', '$'); //replacing s's with $'s and storing string in str
System.out.println(str); //displaying str in console
} used it like this and worked like a charm
you have to store your new string somewhere so you can modify it again, there is no way you can do this in one step.
Creatures
Last edited by Creatures : September 21st, 2008 at 05:25 PM.
|
| |
September 21st, 2008, 05:54 PM
|
#4 (permalink)
| | Junior Member
Join Date: Sep 2008
Posts: 2
|
Thanks a bunch! I'll need to keep that "str = " in mind the next time a problem like this comes up. |
| |
September 24th, 2008, 03:11 PM
|
#5 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,741
|
"str =" is nothing special, just a name for a String variable; could have named it "String HermanMunster" |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |