Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 8th, 2005, 09:26 AM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validating Text in JText

Hello,

   How can i validate users' input in a JText Field as integers?I am new to this, any help is appreciated.

 Thanks,
 
Old June 8th, 2005, 11:50 AM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry,

  Got it working.
   After trying many different things, this got it working:



public boolean simple_validate(String theStr){

   char strmyStr = theStr.tocharArray();

   for(int i=0; i < strmyStr.length; ++i){
       if(Character.isDigit[i]){
                 return true;
       }
                 return false;
    }
}

What do you gurus think? is this a good approach to solving the problem? or should i consider something else more compact?

 Thanks


 
Old June 21st, 2005, 11:26 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

This would return true if the second(you're pre-incrementing the loop index) character was a digit, even if the rest of the String contained non-digits.

If you're allowing longer (than 2 char) Strings it'd be better to do:

public boolean simple_validate(String theStr){
    char strmyStr = theStr.tocharArray();

    for(int i=0; i < strmyStr.length; i++){
       if(!Character.isDigit[i]){ // if character is NOT a digit, return false
             return false;
       }
    }
    return true; // if we get here, all the characters are digits
}

For very long Strings you might also want to look at using String.charAt(x), and save on the overhead of creating a character array:

public boolean simple_validate(String theStr){
    for(int i=0; i < theStr.length(); i++){
       if(!theStr.charAt(i).isDigit()) return false;
    }
    return true;
}


--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com
 
Old June 23rd, 2005, 08:35 AM
Registered User
 
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

correct code£º
public boolean simple_validate(String theStr)
    {

        char[] strmyStr = theStr.toCharArray();

        for (int i = 0; i < strmyStr.length; ++i)
        {
            if (!Character.isDigit(strmyStr[i]))
            {
                return false;
            }
        }
        return true;
    }

blackcow





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validating a text box maitias C# 2005 2 March 25th, 2007 01:52 AM
[:D]Simple Method of Validating text boxes[:D] samnikh VB Databases Basics 0 June 29th, 2005 09:26 PM
Validating Template Text box aleahy BOOK: Beginning ASP.NET 1.0 0 November 11th, 2003 11:22 AM
validating rich text field for attachment Mimi Javascript How-To 4 August 25th, 2003 03:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.