home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2526
Discussions: 186,605, Posts: 2,227,032, Members: 230,239
Free Scan: Update Your PC's Outdated Drivers to Optimize Performance
Old September 21st, 2008, 03:50 PM   Digg it!   #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"

murakumo32 is offline   Reply With Quote
TechIMO.com Ads - Login or register for less ads.
How many errors does your computer have?

You no longer need to guess! This free stability scan and registry cleaner download will give you a complete diagnosis of your Windows registry, identifying errors and conflicts.

FREE instant scan


Guest, Register Free! to remove this ad and get your tech support questions answered in minutes!
Old September 21st, 2008, 03:58 PM     #2 (permalink)
Go back to sleep
 
Creatures's Avatar
 
Join Date: Jul 2002
Location: Switzerland
Posts: 6,052
Send a message via ICQ to Creatures
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

Creatures is offline   Reply With Quote
Old September 21st, 2008, 05:21 PM     #3 (permalink)
Go back to sleep
 
Creatures's Avatar
 
Join Date: Jul 2002
Location: Switzerland
Posts: 6,052
Send a message via ICQ to Creatures
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.
Creatures is offline   Reply With Quote
Old 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.
murakumo32 is offline   Reply With Quote
Old September 24th, 2008, 03:11 PM     #5 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,741
Send a message via AIM to Rootstonian
"str =" is nothing special, just a name for a String variable; could have named it "String HermanMunster"
Rootstonian is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I disable the "power", "sleep", and "wake" buttons. ShawnD1 General Tech Discussion 1 January 30th, 2004 11:50 AM
IE not supporting "Java"?? Sweeper Technical Support 5 February 9th, 2003 01:54 PM
Remember the "Worm", "Cave", or "Ribbon" games? SickPup404 IMO Community 11 July 10th, 2002 08:02 PM
How to eliminate "java script error"?? chapmc Technical Support 4 April 8th, 2002 09:06 AM

Most Active Discussions
Is It Just Me? (524)
Misery Loves Company... (1849)
Why Does the MOON Grow Bigger as It.. (17)
heatsink issue (10)
New Mobo (18)
UPGRADING C/D DRIVE TO 250GB & .. (13)
SSD's, RAID, and External Backup (7)
1 internet. 1 house. 3 computer. ho.. (13)
Is This A Compatible Gaming PC? (18)
Recent Discussions
UPGRADING C/D DRIVE TO 250GB &a.. (13)
PNY vs Gainward (3)
Linksys WRT54G (2)
Contest: Logo and Banner design.. (0)
Hotmail Q (2)
SSD's, RAID, and External Backu.. (7)
Laptop doesn't stay connected t.. (7)
Computer won't start (4)
Official World of Warcraft Thre.. (4528)
FS: Dell 6000 laptop, modded 36.. (2)
Apple iPod touch 16 GB $200 (4)
Six 28-Disc Cross Design Black .. (4)


All times are GMT -4. The time now is 11:11 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28