Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java txt box valadation?

  • 17-01-2004 12:55AM
    #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,342 ✭✭✭OfflerCrocGod


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


  • Registered Users, Registered Users 2 Posts: 6,342 ✭✭✭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