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 4 of 4

Thread: How to check whether the string contains only the specified characters ????

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to check whether the string contains only the specified characters ????

    Hi,

    I may get more than 1000 lines in a file. Each line ends with a new line character. Sometimes the file may have 4 lines.
    Other lines will have characters such as , : alone.

    I'm storing each line in the database if it is a valid line and If the line contains only the above characters (, : ) i need to skip the line and doesn't want to save.

    Example Input,

    Allows you to work and play in a secure computing environment.
    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    Allows you to play online
    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::

    Lines Saved:
    Allows you to work and play in a secure computing environment.
    Allows you to play online

    Currently what i'm skipping the line if it has the above characters as below

    Pattern.compile([,:+ ])
    Matcher matcher = pattern.matcher(string);
    String tmp = matcher.replaceAll("")
    if(tmp.length > 0 ) {
    /// Skip
    }

    Is there is any other way to do instead of calling replaceAll ??

    I like to know whether is there is any other way to check whether the string contains only those characters and nothing else, so that i can directly skip
    instead of calling replaceAll and checking for length?

    any pattern or regular expression which should say the line contains only those characters and nothing else, so we can skip the line.

    Please help.

    Thanks,
    Kathir


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to check whether the string contains only the specified characters ????

    Please post your code for us to see..

    You could use an 'if' statement to see if the String contains the unwanted characters, If it does, skip, else.. write to file.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to check whether the string contains only the specified characters ????

    I should skip the line if the string contains "ONLY" the unwanted characters...

    If the line has some characters and unwanted characters..then it is fine..

    Ex:

    Programming languae,,,,,,, --> Accepted
    ,,,,,,,,,,,, --> Rejected
    This , + : comes with a --> Accepted

    Please let me know.

    Thanks,
    Kathir

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to check whether the string contains only the specified characters ????

    The following regular expressions should work
    if ( string.matches("[,;]*") ){
    //skip
    }

    This will also skip empty lines as well, so change the '*' to a '+' if you do not want to skip those for whatever reason

Similar Threads

  1. Check for palindrome numbers
    By SnooSnoo in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 4th, 2010, 06:11 PM
  2. Can Anyone Check This Link
    By arpitgadle in forum Java Servlet
    Replies: 5
    Last Post: October 7th, 2009, 08:56 AM
  3. Certain Chinese Characters not displayed properly.
    By kerwintang in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 20th, 2009, 08:23 AM
  4. Replies: 5
    Last Post: May 21st, 2009, 02:45 AM
  5. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM