Results 1 to 5 of 5
  1. #1
    jojanny is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default Limiting only one character in a string field

    Hi guys,,

    i'm new and still learning java, i was wondering i'm trying to limit a certain set of alphabets in a string field. For example, i'm having someone to input something, like a "this team vs that team" into a "team - team"

    so when someone inputs it, i just want to limit only one "-" . So if he tries to put more than one - , the procedure doesnt work. But only limits to allow one " - " how do i do that?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What is the context? Is this a GUI? If so, would it be better to have two fields, one for team1 and one for team 2, so there's no need to have a dash (or so the dash is present in a label between the text fields)? If not a GUI, then again you could have them add each team on a separate line.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I agreed. Most of those kind of validation could be handle through the design sense using the UI. Can you explain a bit more details.

    And also do you know about events like keypress, keyup, keydown and related in any language, not specifically with Java.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Quote Originally Posted by Eranga View Post
    And also do you know about events like keypress, keyup, keydown and related in any language, not specifically with Java.
    A KeyListener is most certainly *not* the way to go. How do you control input that is pasted in?

    While Fubarable's suggestion of two fields with a label in between is what I would go with (and have gone with, in the past, just not in Java), the correct answer to the wrong approach postulated by the OP is to use a DocumentFilter.

    db

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 2
    Last Post: 08-21-2010, 10:53 AM
  2. compare String to the character .
    By Bulelakes in forum JDBC
    Replies: 12
    Last Post: 07-28-2010, 11:35 AM
  3. Taking a character off the end of a string.
    By AJArmstron@aol.com in forum New To Java
    Replies: 5
    Last Post: 05-05-2010, 02:56 AM
  4. Limiting loop in boolean string
    By jimmy-lin in forum New To Java
    Replies: 7
    Last Post: 10-10-2009, 06:31 AM
  5. how to get next character/string
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-28-2009, 04:04 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
  •