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

    Default How do I make this Letter Remover Program allow me to enter a letter to remove (BlueJ

    I am trying to write this program for school and I am not sure how to allow the user to input a letter. I tried compiling the program and it would not work because I can't just let a person type a "char" with the scanner straightforwardly like how you can do with an int variable. Help! This is for a grade! My teacher gave me the algorithm in English, so this should help you answer the question:

    int loc = x.indexOf(y)

    while there are more letters to remove
    {
    take out the letter ( substring )
    loc = x.indexOf(y)
    }

    Java Code:
    import java.util.Scanner;
    public class Lawl
    {
    
        public static void main(String[]args) 
        {
    		
    		char c;
    		
            Scanner scan = new Scanner(System.in);
    		String strValue = scan.nextLine();
    		int i = strValue.indexOf(c);
    		// call a method to delete all occurrences
    	   c = scan.nextLine().charAt(i);
    		String newString = deleteAll(strValue,  c);
    		// print the new string value
    		System.out.println(newString);
    		
    
    	}
    
    	private static String deleteAll(String strValue, String charToRemove) {
    		return strValue.replaceAll(charToRemove, "");
    
    	}
    }

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

    Default Re: How do I make this Letter Remover Program allow me to enter a letter to remove (B

    type a "char" with the scanner straightforwardly
    Yes the Scanner class does not have a method to read a single char. You can read a String and use the String class's method to get char from that String.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: 01-29-2016, 09:11 PM
  2. Replies: 11
    Last Post: 06-19-2013, 09:45 AM
  3. Random-Letter Decipher Program Problems.
    By ssrock64 in forum New To Java
    Replies: 0
    Last Post: 02-20-2013, 11:37 PM
  4. Letter-Shifting Program
    By jhurley03 in forum New To Java
    Replies: 2
    Last Post: 10-13-2011, 07:53 AM
  5. Letter counter program. having trouble understanding it.
    By silverglade in forum New To Java
    Replies: 17
    Last Post: 05-21-2011, 01:06 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
  •