Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Newbie, please help

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Newbie, please help

    I'm new to java, so please help. I'm trying to write this program that ignores unwanted characters in a string. For example,

    if I ask for, and user types in : t!es@#t
    it's going to ignore the "!@#", and prints just the "test"

    I'm all out of ideas, please help. I'm using Eclipse IDE.

    package chapterFour;
     
    import java.util.Scanner;
     
    public class tony {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner(System.in);
     
    		String word, ignore, newWord;
     
    		System.out.print("Enter a word to clean up : ");
    		word = scan.nextLine();
     
    		ignore = "~!@#$%^&*()_+-";
     
    		for(int a=0; a<word.length()-1; a++)
    		{
    			for(int b=0; b<ignore.length()-1; b++)
     
    				if(word.charAt(a) != ignore.charAt(b))
    				{
    					newWord = word.substring(b);
     
    				}
     
    		}
     
    		System.out.print(newWord);
     
    	}
     
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    589
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Newbie, please help

    word = word.replaceAll("!@#", ""); ??

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Newbie, please help

    Thank you

    --- Update ---

    Its' better, but not what I want . Now, if I type in "ignore" letters in sequence, I get the results I want. If I don't type them in sequence, I don't get what I want.

    Example:

    My ignore strings are "#&"

    so if I type #&tony, I'll get tony (what I want),
    but if I type &#tony, I get &#tony (not what I want).
    if I type &tony, I get &tony
    How can I make it so, it will ignore any strings in my ignore strings whether it's in sequence of if not.

    package chapterFour;
     
    import java.util.Scanner;
     
    public class tony {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner(System.in);
     
    		String word, ignore, newWord;
     
    		ignore = "#&";
     
    		System.out.print("Enter a word to clean up : ");
    		word = scan.nextLine();
     
    		newWord = word.replace(ignore, "");
     
    		System.out.println(newWord);
    	}
    }

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    My Mood
    Inspired
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: Newbie, please help

    You can simplify substantially your code by using regular expressions.

                    String word = "&$t-o&$n(y)";
    		String ignore = "[~!@#$%^&*()_+-]";
    		word = word.replaceAll(ignore, "");
    		System.out.println(word);

    Note that in the ignore String the characters that you wish to remove from the word String are contained within [ and ]. The method replaceAll will search the entire word String looking for occurrences of the each character in the ignore String and replace it with an empty String.

    Theres a good tutorial on regexp here.

  5. The Following User Says Thank You to atheedom For This Useful Post:

    GregBrannon (July 5th, 2013)

  6. #5
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Newbie, please help

    Thank you it works

Similar Threads

  1. Really A newbie :)
    By killerspy08 in forum Member Introductions
    Replies: 2
    Last Post: August 19th, 2012, 02:00 AM
  2. I'm a newbie
    By chanukya4528 in forum Member Introductions
    Replies: 0
    Last Post: July 5th, 2012, 10:04 AM
  3. Newbie
    By gavlaa in forum Member Introductions
    Replies: 1
    Last Post: May 24th, 2011, 07:00 AM
  4. Hi, newbie here!!
    By niecah in forum Member Introductions
    Replies: 15
    Last Post: April 28th, 2010, 04:21 AM
  5. I'm a newbie
    By r12ki in forum Member Introductions
    Replies: 2
    Last Post: June 1st, 2009, 06:38 AM