Results 1 to 6 of 6
  1. #1
    potanta is offline Member
    Join Date
    Jan 2016
    Posts
    5
    Rep Power
    0

    Default 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);   
           
           }
            
            
            
    
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to fix this Letter Remover program? Is my algorithm correct?

    Is my algorithm correct?
    Could you post your algorithm (in English) so we can see it? Normally the algorithm for a complicated section of code is added to the code as comments.
    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.

  3. #3
    potanta is offline Member
    Join Date
    Jan 2016
    Posts
    5
    Rep Power
    0

    Default 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);
    }

  4. #4
    potanta is offline Member
    Join Date
    Jan 2016
    Posts
    5
    Rep Power
    0

    Default 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);
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

  6. #6
    camel-man is offline Member
    Join Date
    Jan 2016
    Posts
    19
    Rep Power
    0

    Default Re: How to fix this Letter Remover program? Is my algorithm correct?

    Another way to do this is use StringBuilder and the method deleteCharAt(). This way you will avoid all the substrings and concatenation.

Similar Threads

  1. Recursion algorithm correct?
    By Spinz in forum New To Java
    Replies: 6
    Last Post: 07-21-2013, 10:40 AM
  2. Random-Letter Decipher Program Problems.
    By ssrock64 in forum New To Java
    Replies: 0
    Last Post: 02-21-2013, 12:37 AM
  3. Letter-Shifting Program
    By jhurley03 in forum New To Java
    Replies: 2
    Last Post: 10-13-2011, 08:53 AM
  4. Letter counter program. having trouble understanding it.
    By silverglade in forum New To Java
    Replies: 17
    Last Post: 05-21-2011, 02:06 PM
  5. Moleskinsoft's Clone Remover
    By Cleaner007 in forum Forum Lobby
    Replies: 3
    Last Post: 10-28-2008, 10:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •