Results 1 to 4 of 4
  1. #1
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Default Want to ensure that only a word is entered?

    Creating a project that I can make a dictionary. Anyway, how do I ensure that only words are put in? These must be a-z, A-Z only? Oh, and also a hyphen for such words like Ice-cream. Howver, at the minute, my program doesn't let me enter "hello". I must be going wrong somewhere in my pattern! Can someone fix this? Also, can ye possibly modify the pattern to allow for hyphens? Thanks!
    Java Code:
    import javax.swing.JOptionPane;
    public class paperWork
    {
    	public static void main(String[]args)
    	{
    		String pattern = "[a-zA-z]";
    		String input = "";
    		boolean cont = false;
    		while (!cont)
    		{
    			input = JOptionPane.showInputDialog(null,"Please enter the word you'd like to add to the dictionary: ");
    			if(input.contains(" ") || input == null || input == "" || (!(input.matches(pattern))))
    				JOptionPane.showMessageDialog(null,"Input incorrect");
    			else
    			cont = true;
    		}
    		System.out.println("This input is correct.");
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,371
    Rep Power
    9

    Default Re: Want to ensure that only a word is entered?

    You pattern only matches a single character. And your second range is incorrect. You want to match one or more characters. So do this:

    Java Code:
     String pattern = "[a-zA-Z]+";
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Default Re: Want to ensure that only a word is entered?

    Hey Jim! :D How exactly does that piece of code work? I don't get the + sign. Could you explain please? :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default Re: Want to ensure that only a word is entered?

    Quote Originally Posted by Denis2k11 View Post
    Hey Jim! :D How exactly does that piece of code work? I don't get the + sign. Could you explain please? :)
    That is regular expression syntax; read all about it in the Pattern class API documentation.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. How to define what a word is for a word count?
    By Fluxtime in forum New To Java
    Replies: 1
    Last Post: 12-03-2012, 11:59 AM
  2. Replies: 3
    Last Post: 08-30-2011, 04:44 PM
  3. Replies: 3
    Last Post: 04-27-2010, 12:40 PM
  4. ensure connectivity
    By perdoname in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 02:29 AM

Posting Permissions

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