Results 1 to 8 of 8
- 11-03-2010, 06:33 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
I need help doing a small program.
Hi guys, I'm new here. I need some help doing a program and I wondered if any of you likes a tiny challenge! I need to design a program that does the following:
1. Reads a whole phrase ending with the word "fi" (it's in catalan).
2. It then needs to read 2 seperate letters.
3. Change all of the letters equal to the first one, to the second one.
4. Print phrase.
Example:
In: "My name is not Jeremy fi" e u
Out: "My namu is not Jurumy fi
Can someone lend me a hand please? thnx!
- 11-03-2010, 06:42 PM #2
Is this a homework assignment? Do you have restrictions on what functions you can and cannot use? There is a replaceAll() method in Java that could be quite handy, but I'm not sure if you're supposed to design your own method to do this.
Either way, have you done input before? You will want to read up on Scanners and input in general. That way you can accept the statement + letters inputted. Then, you can use println() to output the new sentence.
- 11-03-2010, 06:49 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
It IS homework and I've been told that I can't use any character replacement method. And yes I have already worked with scanners and, of course, I'm familiar with System.out.println...BUT I don't know what to do about the replacing thing. Can you help me please?
- 11-03-2010, 06:54 PM #4
You'll want to start by creating a method that accepts three strings--one sentence, one character to replace, and one to replace it with.
Next, you want to create a blank string, then loop through the existing sentence from start to finish (using a for loop and charAt()), and add the character to the blank string if it's NOT equal to the one you want to replace, or add the replaced character if it IS.
Does that make sense? (I have to head to class for a bit but I'll check back in a couple hours to see how you're making out.)
- 11-03-2010, 07:04 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 25
- Rep Power
- 0
Hi !!!
you can:
1. Use length to determine what is the string's size.
2. Read the string inside a loop and add the element to array but changing the characters you need to change.
3. Read the array inside a looop and create a new string.
4. Show the string.
Hope be useful.
- 11-03-2010, 07:53 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
I'm going to try and use indexOf to find all of the positions and replace them. I'll reply back!
- 11-03-2010, 09:14 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Zack I ended up doing what you said and it ended up being really short and neat so thnx for the help! :)
- 11-03-2010, 10:00 PM #8
You're welcome! If you had used indexOf you would have issues with substring() and such that would have been messy. Glad you have it working!
Similar Threads
-
Small calender program
By Moustafa taha in forum New To JavaReplies: 1Last Post: 10-19-2010, 07:52 PM -
Small yahtzee program
By kimmelim in forum New To JavaReplies: 20Last Post: 03-12-2009, 12:11 PM -
Small Dice Program
By kimmelim in forum New To JavaReplies: 13Last Post: 02-15-2009, 01:01 AM -
Please HELP Java small program
By afrttoh in forum New To JavaReplies: 14Last Post: 11-08-2008, 02:29 AM -
small issues with a program
By jimJohnson in forum New To JavaReplies: 6Last Post: 04-25-2008, 08:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks