Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java txt box valadation?

  • 16-01-2004 11:55pm
    #1
    Closed Accounts Posts: 1,637 ✭✭✭


    OK I need to valadate a InputDialog box so it can only accept a number, so no letters or letters&numbers.

    Heres what I got so far,


    import javax.swing.JOptionPane;

    public class valadation {

    public static void main(String[] args) {

    int num;
    String choice;
    int result;

    choice = JOptionPane.showInputDialog(
    "Enter Number");

    while (choice == ""){
    JOptionPane.showMessageDialog(null,
    "Error, Numbers only");
    }

    while (choice == null){
    JOptionPane.showMessageDialog(null,
    "Error, Numbers only");

    choice = JOptionPane.showInputDialog(
    "Enter Number");

    }


    while (choice.matches("\\D+")){

    JOptionPane.showMessageDialog(null,
    "Error, Numbers only");

    choice = JOptionPane.showInputDialog(
    "Enter Number");
    }


    num = Integer.parseInt(choice);

    result = square(num);

    JOptionPane.showMessageDialog( null,
    "Result = " + result, "Result",
    JOptionPane.PLAIN_MESSAGE );

    //System.out.print(result);

    System.exit( 0 );

    }

    public static int square(int x){//Method Call

    return x * 6;

    }

    }


    OK it works if I enter a number, and gives the error message if I enter a letter, but any other combination it crashes???

    Theres also a method you can ignore that. "Sorry"

    Thanks loads in advance........

    Thanks joePC


Comments

  • Registered Users, Registered Users 2 Posts: 6,336 ✭✭✭OfflerCrocGod


    Jesus boy use the Force!!!....RegExp (Regular Expresions).


  • Registered Users, Registered Users 2 Posts: 6,336 ✭✭✭OfflerCrocGod


    import javax.swing.JOptionPane;

    public class valadation {

    public static void main(String[] args) {

    int num;
    String choice;
    int result;

    choice = JOptionPane.showInputDialog("Enter Number");

    while ( !choice.matches("[0-9]+" ) ){
    JOptionPane.showMessageDialog(null,"Error, Numbers only");
    choice = JOptionPane.showInputDialog("Enter Number");
    }

    num = Integer.parseInt(choice);
    result = square(num);

    JOptionPane.showMessageDialog( null,"Result = " + result, "Result", JOptionPane.PLAIN_MESSAGE );

    System.exit( 0 );
    }

    public static int square(int x){//Method Call
    return x * 6;
    }
    }

    Should work, test it.


  • Closed Accounts Posts: 1,637 ✭✭✭joePC


    Thanks alot OfflerCrocGod,

    Its always the small things :)

    ( !choice.matches("[0-9]+" ) )

    Thanks joePC


Advertisement