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: Input Validation

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

    Red face Input Validation

    Hi there, can anyone help me to validate the input value? example when i pressed character on the keyboard it will ask us to press number only.. anyone got any idea how to validate it?
    thanks in advance...

    import javax.swing.*;
     
    public class additiontutor
    {
     public static void main(String args[])
     {
      int number1 = (int)(System.currentTimeMillis() % 10);
      int number2 = (int)(System.currentTimeMillis() * 7 % 10);
     
      String answerString = JOptionPane.showInputDialog
        ("What is " + number1 + " + " + number2 + "?");
     
      int answer = Integer.parseInt(answerString);
     
      JOptionPane.showMessageDialog(null,
        number1 + " + " + number2 + " = " + answer + " is " +
        (number1 + number2 == answer));
     
     }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Input Validation

    Several ways to do this depending upon the behavior you want. First you can just validate the user input before using Integer.parseInt (see http://www.javaprogrammingforums.com...tring-int.html) and if its wrong respond accordingly (warn the user, re-show the dialog, etc...). Another way is to create a custom input dialog where you validate input in-line (see How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) - the CustomDialog.java example). Lastly you can create a a custom JTextField that you add to a custom JOptionPane that only allows the input you want (digits) using a KeyListener to screen the input, perhaps responding with a System beep when invalid input is typed

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

    Red face Re: Input Validation

    Dear copig, can you show me how to do it based on my program?

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

    Default Re: Input Validation

    Quote Originally Posted by nic View Post
    Dear copig, can you show me how to do it based on my program?
    I didn't post code because of the several different ways to do it, the latter two are a bit complex. So here's the first possible way in pseudo-code

     
    public void someFunction(){
        String input = JOptionPane.showInputDialog(/*your dialog stuff*/);
        while ( !isDigits(input) ){
            System.out.println("Invalid Input");
             input = JOptionPane.showInputDialog(/*your dialog stuff*/);
        }
        ///deal with proper input here
    }
    public boolean isDigits(){
    //check for proper digits (see link in my original post), return true for all digits, false otherwise
    }

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

    Default Re: Input Validation

    sorry for the wrong spelling of your name just now... err..if based on my program there it why have to use boolean ?? blur blur .... i have view the tutorial but it did not show how to make it also...

Similar Threads

  1. Problems with If validation
    By websey in forum Loops & Control Statements
    Replies: 1
    Last Post: November 18th, 2009, 08:43 AM
  2. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 02:46 AM
  3. Difference between input.next and findInLine(".")charat(0)
    By lotus in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 6th, 2009, 05:10 AM
  4. Different operation on Array
    By jempot in forum Collections and Generics
    Replies: 4
    Last Post: January 27th, 2009, 05:07 AM
  5. Program to mask input
    By sah in forum Java Theory & Questions
    Replies: 1
    Last Post: January 26th, 2009, 05:43 PM