Results 1 to 6 of 6
- 01-29-2016, 03:59 AM #1
Member
- Join Date
- Jan 2016
- Posts
- 5
- Rep Power
- 0
How to fix this Letter Remover program? Is my algorithm correct?
So I have to write a program which is called "Letter Remover" in BlueJ. It is supposed to remove letters when the user types in the letter. So the program has to run with a user inputting any word they want, then they will be asked to type a letter from that word they want to remove. For example if I type the word "aaabcdef" and then i type "a" when asked to type a letter, then it should output as "bcdef". Is my algorithm correct? And by the way, I get an error from BlueJ, saying that variable c might not have been initialized. Also, what do I write in the parenthesis "System.out.println()" to print out the modified word (meaning the word after a character has been deleted)
Java Code:import java.util.Scanner; /** * Write a description of class Runner here. * * @author (your name) * @version (a version number or a date) */ public class Runner { public static void main(String[]args) { Scanner scan = new Scanner(System.in); String word = ""; System.out.println("Enter a word."); word = scan.nextLine(); System.out.println("Enter a letter to remove."); char c; int i = word.indexOf(c); int j = 0; c = scan.nextLine().charAt(i); for (i= word.indexOf(c); i >= word.indexOf(c); i++) { word.substring(i, i); i = word.indexOf(c); } System.out.println(word); } }
- 01-29-2016, 04:06 AM #2
Re: How to fix this Letter Remover program? Is my algorithm correct?
Is my algorithm correct?
Better would be for you to thoroughly test it to see if the code does what you want.
Note: The substring method returns a String. The code is ignoring the returned String.Last edited by Norm; 01-29-2016 at 04:08 AM.
If you don't understand my response, don't ignore it, ask a question.
- 01-29-2016, 05:07 AM #3
Member
- Join Date
- Jan 2016
- Posts
- 5
- Rep Power
- 0
Re: How to fix this Letter Remover program? Is my algorithm correct?
Well, I have to do this for school, so my teacher gave me a sheet with a little algorithm in it. I am not sure what he means:
int loc = x.indexOf(y);
while there are more letters to remove
{
take out the letter ( substring);
loc = x.indexOf(y);
}
- 01-29-2016, 05:11 AM #4
Member
- Join Date
- Jan 2016
- Posts
- 5
- Rep Power
- 0
Re: How to fix this Letter Remover program? Is my algorithm correct?
Here is the algorithm my teacher gave me:
int loc = x.indexOf(y);
while there are more letters to remove
{
take out the letter ( substring);
loc = x.indexOf(y);
}
- 01-29-2016, 02:31 PM #5
Re: How to fix this Letter Remover program? Is my algorithm correct?
The names in that code make it harder to understand.
I assume x is the String with the letters to be removed
and y is the String with the single letter that is to be removed.
loc has a good name.
Given that algorithm, you need to expand the logic for removing letter at the position loc using the substring method.
For example get the String before loc and the String after loc and concatenate them to make a new String.If you don't understand my response, don't ignore it, ask a question.
- 01-29-2016, 10:11 PM #6
Member
- Join Date
- Jan 2016
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Recursion algorithm correct?
By Spinz in forum New To JavaReplies: 6Last Post: 07-21-2013, 10:40 AM -
Random-Letter Decipher Program Problems.
By ssrock64 in forum New To JavaReplies: 0Last Post: 02-21-2013, 12:37 AM -
Letter-Shifting Program
By jhurley03 in forum New To JavaReplies: 2Last Post: 10-13-2011, 08:53 AM -
Letter counter program. having trouble understanding it.
By silverglade in forum New To JavaReplies: 17Last Post: 05-21-2011, 02:06 PM -
Moleskinsoft's Clone Remover
By Cleaner007 in forum Forum LobbyReplies: 3Last Post: 10-28-2008, 10:54 PM
Bookmarks